8

自己結合を介して同じモデルのレコード間に複数の関係を実装しようとしています(@Shtééfの回答に基づく)。私は次のモデルを持っています

create_table :relations, force: true do |t|
  t.references :employee_a
  t.string     :rel_type
  t.references :employee_b
end

class Relation < ActiveRecord::Base
  belongs_to :employee_a, :class_name => 'Employee'
  belongs_to :employee_b, :class_name => 'Employee'
end

class Employee < ActiveRecord::Base
  has_many :relations, foreign_key: 'employee_a_id'
  has_many :reverse_relations, class_name: 'Relation', foreign_key: 'employee_b_id'

  has_many :subordinates, through: :relations, source: 'employee_b', conditions: {'relations.rel_type' => 'manager of'}
  has_many :managers, through: :reverse_relations, source: 'employee_a', conditions: {'relations.rel_type' => 'manager of'}
end

この設定により、各レコードの部下とマネージャーのリストに正常にアクセスできます。しかし、私は次のように関係を築くのに苦労しています

e = Employee.create
e.subordinates.create
e.subordinates #=> []
e.managers.create
e.managers #=> []

問題はそれが関係のタイプを設定しないということです、それで私は書かなければなりません

e = Employee.create
s = Employee.create
e.relations.create employee_b: s, rel_type: 'manager of'
e.subordinates #=> [#<Employee id:...>]

私は何か間違ったことをしていますか?

4

4 に答える 4

8

has_manyアソシエーションbefore_addでコールバックを使用できます:before_remove

class Employee < ActiveRecord::Base
  has_many :relations, foreign_key: 'employee_a_id'
  has_many :reverse_relations, class_name: 'Relation', foreign_key: 'employee_b_id'

  has_many :subordinates, 
           through: :relations, 
           source: 'employee_b', 
           conditions: {'relations.rel_type' => 'manager of'}
           :before_add => Proc.new { |employe,subordinate| employe.relations.create(employe_b: subordinate, rel_type: 'manager of') },
           :before_remove => Proc.new { |employe,subordinate| employe.relations.where(employe_b: subordinate, rel_type: 'manager of').first.destroy }

  has_many :managers,  
           through: :reverse_relations, 
           source: 'employee_a', 
           conditions: {'relations.rel_type' => 'manager of'}
           :before_add => Proc.new { |employe,manager| employe.reverse_relations.create(employe_a: manager, rel_type: 'manager of') },
           :before_remove => Proc.new { |employe,manager| employe.reverse_relations.where(employe_b: subordinate, rel_type: 'manager of').first.destroy }

これは機能し、使用できるようになります。コールバックでinstread ofemploye.managers.create
を使用することもできます。また、このソリューションに関するこの質問 を読むこともできます。buildcreate

于 2011-06-25T12:28:36.560 に答える
3

複数の多対多の自己結合アソシエーションを作成するには、接続を管理するために複数のテーブルを用意する方が理にかなっている場合があることをお勧めします。そうすれば、データの観点からは何が起こっているのかが非常に明確になり、論理の観点からも明確になります。したがって、これらの線に沿った何か:

create_table :manage_relation do |t|
  t.references :employee_id
  t.references :manager_id
end
create_table :subordinate_relation do |t|
  t.references :employee_id
  t.references :subordinate_id
end

class Employee < ActiveRecord::Base

  has_many :subordinates, 
           :through => :subordinate_relation, 
           :class_name => "Employee", 
           :foreign_key => "subordinate_id"
  has_many :managers, 
           :through => :manage_relation, 
           :class_name => "Employee", 
           :foreign_key => "manager_id"

  belongs_to :employee, 
             :class_name => "Employee"
end

このように、コーディングの観点から必要以上に複雑になることはなく、標準のコレクションを使用してアクセスでき、管理しなくても接続が適切に設定されます。したがって、これらのコレクションは両方とも機能するはずです。

employee.managers
employee.subordinates

また、他の変数を管理する必要はありません。わかる?テーブルを追加しますが、明瞭さが向上します。

于 2011-06-21T14:07:09.250 に答える
2

次のようにモデルをやり直します。

class ManagerRelation < ActiveRecord::Base
  belongs_to :manager, :class_name => 'Employee'
  belongs_to :subordinate, :class_name => 'Employee'
end

class Employee < ActiveRecord::Base
  has_many :manager_relations,     :class_name => "ManagerRelation",
               :foreign_key => :subordinate_id
  has_many :subordinate_relations, :class_name => "ManagerRelation", 
               :foreign_key => :manager_id

  has_many :managers,     :source => :manager,     
               :through => :manager_relations

  has_many :subordinates, :source => :subordinate, 
               :through => :subordinate_relations
end

これで、次のことができます。

employee.managers
employee.subordinates    
employee.managers << employee2    
employee.subordinates << employee3

注:通常、2人のマネージャーに報告させられた場合、1人が会社を辞めることを示します:-)

于 2011-06-22T05:02:33.150 に答える
2

提示された関係を考えると

create_table :relations, force: true do |t|
  t.references :employee_a
  t.string     :rel_type
  t.references :employee_b
end


class Employee < ActiveRecord::Base

  has_many :subordinate_relations, :class_name => "Relation", :conditions => {:rel_type => 'manager of'}, :foreign_key => :employee_a
  has_many :subordinates, :through => :subordinate_relations, :source => :subordinate, :foreign_key => :employee_b

  has_many :manager_relations, :class_name => "Relation", :conditions => {:rel_type => 'manager of'}, :foreign_key => :employee_b
  has_many :managers, :through => :manager_relations, :source => :manager, :foreign_key => :employee_a

end


class Relation < ActiveRecord::Base

  belongs_to :manager, :class_name => "Employee", :foreign_key => :employee_a
  belongs_to :subordinate, :class_name => "Employee", :foreign_key => :employee_b

end


e = Employee.create
e.subordinates.create #Employee ...
e.subordinates #[<Employee ...]

e2 = Employee.create
e2.managers.create #Employee
e2.managers #[<Employee ...]

解決策は機能しますが、関連付けを「rel_type」と結び付けることで少し混乱しています。この場合-rel_typeは冗長であり、関係は次のようにマップする必要があります。

create_table :relations do |t|
    t.reference :manager
    t.reference :subordinate
end

このような場合、関連付けのマッピングは少し単純にする必要があります。

于 2011-06-25T06:57:34.100 に答える