[英]Rails order collection by attribute of associated model?
我正在试图找出一种基于所收集模型的关联属性来订购模型集合的方法(我想......真是满口)。 这是我正在做的事情:
class Item < ActiveRecord::Base
belongs_to :category
end
class Order < ActiveRecord::Base
has_many :items, :order => :category_id
end
order.items
返回category_id排序的Items。 但我真正想要的是按字母category.name
列出它们。 那可能吗?
谢谢,斯图尔特
它应该工作,如果你使用:include来急切加载类别。
class Order < ActiveRecord::Base
has_many :items, :include => :categories, :order => "categories.name"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.