扶手:使用顺序的has_many / belongs_to的关系 [英] Rails: order using a has_many/belongs_to relationship
本文介绍了扶手:使用顺序的has_many / belongs_to的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能使用find方法来订购的基础上与其他类的类的的has_many关系的结果。例如,
I was wondering if it was possible to use the find method to order the results based on a class's has_many relationship with another class. e.g.
# has the columns id, name
class Dog < ActiveRecord::Base
has_many :dog_tags
end
# has the columns id, color, dog_id
class DogTags < ActiveRecord::Base
belongs_to :dog
end
和我想这样做是这样的:
and I would like to do something like this:
@result = DogTag.find(:all, :order => dog.name)
感谢你。
推荐答案
您需要加入相关表的要求。
You need to join the related table to the request.
@result = DogTag.find(:all, :joins => :dog, :order => 'dogs.name')
注意狗
是复数的:为了
语句的
Note that dogs
is plural in the :order
statement.
这篇关于扶手:使用顺序的has_many / belongs_to的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文