I'm having a little trouble figuring this out: I have a model Machine
that has a foreign key on a locations
table, and I want the default scope of Machine
to sort by location.name
开发者_JAVA技巧. Is this possible?
Yes, use a join to your other table.
class Machine < ActiveRecord::Base
default_scope joins(:location).order('locations.name')
end
Make sure the relation you call in joins
matches the one defined in your Machine
model.
Yes, in your Machine model:
has_many :locations, :order => "name ASC"
精彩评论