[英]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.