【发布时间】:2011-05-30 14:31:56
【问题描述】:
我在弄清楚这一点时遇到了一些麻烦:我有一个模型Machine,它在locations 表上有一个外键,我希望Machine 的默认范围按location.name 排序。这可能吗?
【问题讨论】:
标签: ruby-on-rails sorting activerecord scope
我在弄清楚这一点时遇到了一些麻烦:我有一个模型Machine,它在locations 表上有一个外键,我希望Machine 的默认范围按location.name 排序。这可能吗?
【问题讨论】:
标签: ruby-on-rails sorting activerecord scope
是的,使用连接到您的其他表。
class Machine < ActiveRecord::Base
default_scope joins(:location).order('locations.name')
end
确保您在joins 中调用的关系与您在Machine 模型中定义的关系相匹配。
【讨论】:
是的,在您的机器型号中:
has_many :locations, :order => "name ASC"
【讨论】: