【发布时间】:2019-08-22 16:48:07
【问题描述】:
我们的父子关系是一对一的
parent.child_id = child.id
parent.child_id 很可能也可以更改为不同的 child_id。不删除父级。我们如何自动销毁/删除已被此操作孤立的子记录?
我们的模型如下所示: 父母
class parent < ActiveRecord::Base
belongs_to :child, dependent: :destroy
after_commit :destroy_orphans, on: [:create, :update]
def destroy_orphans
Child.where.not(id: Parent.all.pluck(:child_id)).destroy_all
end
end
我们已经添加了 destroy_orphans 方法来完成此操作,但它增加了可能不需要存在的查询开销。
孩子
class child < ActiveRecord::Base
has_one :parent, inverse_of: :child, dependent: :nullify
before_save { build_parent unless parent && !parent&.child_id.nil? }
end
如果父级的 child_id 发生变化,我们希望触发删除/销毁子级。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4