0
class Item < ActiveRecord::Base
belongs_to :account
belongs_to :category
end
class Category < ActiveRecord::Base
belongs_to :account
has_many :items
end
我想做到以下幾點:Rails 3.1 - 通過belongs_to關聯範圍爲多個對象?
@items = @account.items.where(...)
@categories = @items.categories.order(...)
@items.categories
應通過belongs_to
協會得到的@items
所有類別。最好的我想出來的是:
@categories = @items.map{|item| item.category }
但是沒有一個範圍來管理呢?
感謝您的回答!您的suggesti on會工作,但我想在我的控制器(過濾器,搜索)中動態地更改@items,並希望將它們顯示在關聯的類別中。對不起,我沒有在我的問題中說清楚這一點。但你絕對正確的地圖命令,我應該使用包括。 – chirimoya 2012-01-03 13:29:51