[英]Querying multi level associations in rails
我有如下关联:位置类
class Location < ActiveRecord::Base
has_many :items
end
物品类
class Item < ActiveRecord::Base
belongs_to :location
belongs_to :category
end
分类
class category < ActiveRecord::Base
has_many :items
end
现在我想查询Location.categories(显示所有类别的位置项)。 我怎样才能实现这一目标?
它应该像这样简单:
class Location < ActiveRecord::Base
has_many :items
has_many :categories, :through => :items
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.