該圖顯示了我的數據模型的一部分。我想獲取與用戶關聯的所有項目(通過組織和items_group)。我應該如何更改模型並在控制器中寫入此查詢?使用:通過=>組織我可以得到所有items_groups,但我不知道如何包括一個關係查詢相關項目。通過多個表獲取記錄
class User < ActiveRecord::Base
has_and_belongs_to_many :organizations
has_many :items_groups, :through => :organizations
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :items_groups
has_many :items, :through => :items_groups
end
class ItemsGroup < ActiveRecord::Base
has_many :items, :inverse_of => :items_group
has_and_belongs_to_many :organizations
has_many :users, :through => :organizations
end
工作!非常感謝... – migu