gpt4 book ai didi

ruby-on-rails - ActiveRecord 中多个自连接的多对多关联

转载 作者:行者123 更新时间:2023-12-04 05:32:07 25 4
gpt4 key购买 nike

我正在尝试通过自联接(基于 @Shtééf's answer )在同一模型的记录之间实现多种关系。我有以下型号

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:...>]

难道我做错了什么?

最佳答案

您可以使用 before_addbefore_remove has_many 关联的回调:

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 }

这应该有效并使您能够使用 employe.managers.create您可能想使用 build代替 create在回调中
您也可以阅读 this question关于这个解决方案

关于ruby-on-rails - ActiveRecord 中多个自连接的多对多关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6351111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com