简体   繁体   English

查询rails中的多级关联

[英]Querying multi level associations in rails

I have associations as below: Location Class 我有如下关联:位置类

class Location < ActiveRecord::Base
  has_many :items
end

Item class 物品类

class Item < ActiveRecord::Base
  belongs_to :location
  belongs_to :category
end

Category class 分类

class category < ActiveRecord::Base
  has_many :items
end

Now i want to query Location.categories (show all categories of location items). 现在我想查询Location.categories(显示所有类别的位置项)。 How can i achieve this? 我怎样才能实现这一目标?

It should be as simple as this: 它应该像这样简单:

class Location < ActiveRecord::Base
  has_many :items
  has_many :categories, :through => :items
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM