【发布时间】:2017-02-21 14:22:45
【问题描述】:
has_many 关系更新并销毁记录时,不会触发 after_commit 回调。
我有关系
class Expertise
has_many :doctor_expertises
has_many :doctor_profiles, through: :doctor_expertises
class DoctorExpertise
belongs_to :doctor_profile
belongs_to :expertise
after_commit :do_something
def do_something
# not called when record destroyed
end
在我的控制器中,我使用以下方法更新has_many 关系
def create
doc = DoctorProfile.find(params[:doctor_id])
doc.expertise_ids = params[:expertise_ids].select do |x|
x.to_i > 0
end
doc.save!
render json: doc.expertises
end
我知道我应该在关系上使用update 和destroy。但是,为什么 after_commit 在记录被销毁时没有被调用?
我猜这与我设置doc.expertise_ids 不触发回调的方式有关。但是,除了简短的here 之外,我找不到有关此方法的任何文档。是否有文件证实或否认这种怀疑,或者还有其他事情发生?
【问题讨论】:
标签: ruby-on-rails