0
私はこのチュートリアルhttp://cobwwweb.com/bi-directional-has-and-belongs-to-many-on-a-single-model-in-railsを、各子供の複数の親との親子関係を行うために使っています。Ruby on Rails:Has_many自己参照 - オブジェクトを表示する
私はすでに親を子と関連付けることができます。
class PerformanceIndicator < ActiveRecord::Base
has_many :improvement_actions
has_ancestry
has_many :left_parent_associations, :foreign_key => :left_parent_id, :class_name => 'PerformanceIndicatorAssociation'
has_many :left_associations, :through => :left_parent_associations, :source => :right_parent
has_many :right_parent_associations, :foreign_key => :right_parent_id, :class_name => 'PerformanceIndicatorAssociation'
has_many :right_associations, :through => :right_parent_associations, :source => :left_parent
def associations
(left_associations + right_associations).flatten.uniq
end
end
そして、これが私のPerformanceIndicatorAssociationモデルです:
class PerformanceIndicatorAssociation < ActiveRecord::Base
belongs_to :left_parent, :class_name => 'PerformanceIndicator'
belongs_to :right_parent, :class_name => 'PerformanceIndicator'
end
どのようにすることができます。しかし、今、私は両親とその子供たち
を一覧表示することができます。これは、私のPerformanceIndicatorモデルであるかを理解していません両親とその子供をこのようにリストアップするか? left_associations
を想定し
Parent1
Child1
Child2
Parent2
Child1
Child2
_Sidenote:_これは決して 'has_and_belongs_to_many'関係ではなく、2つの独立した' has_many'関係です。 – mudasobwa