2013-04-16 52 views
0

我有如下的關聯: 位置類查詢的多級協會

class Location < ActiveRecord::Base 
    has_many :items 
end 

Item類

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

Category類

class category < ActiveRecord::Base 
    has_many :items 
end 

現在我想查詢位置。類別(顯示所有類別的位置項目)。 我怎樣才能做到這一點?

回答

2

它應該是如此簡單:

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

感謝它的工作:)。 – geekdeepak