이미지가 내 데이터 모델의 일부를 보여줍니다. 조직 및 items_group을 통해 사용자와 관련된 모든 항목을 가져 오려고합니다. 모델을 어떻게 변경하고 컨트롤러에서이 쿼리를 작성해야합니까? Using : through => organizations 모든 items_groups를 얻을 수는 있지만 관련 항목을 쿼리하기 위해 하나의 관계를 추가하는 방법은 없습니다.여러 테이블을 통해 레코드를 가져 오는 중
users.organizations.item_groups.items
비록 그것을이 포함되어야 모델을 작동하려면 :
class User < ActiveRecord::Base
has_many :organizations, :through => :organization_users
end
class Organization < ActiveRecord::Base
has_many :item_groups, :through => :items_groups_organizations
end
class ItemsGroup < ActiveRecord::Base
belongs_to :item
end
희망을 당신이 관계는이 작업을해야합니다 귀하의 모델 설정하면
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