Triggering dependent: :destroy with overridden destroy-method

旧街凉风 提交于 2020-01-31 10:35:03

问题


In our application we've overridden the ActiveRecord destroy method so that our records do not get deleted (so the user can undelete). Like so:

def destroy
  self.is_deleted = true
  self.save
  freeze
end

However, this seems to have disabled the dependent destroy on our has_many relationships. In other words, if destroy is called on a parent object, the child objects of has_many do not get destroyed (they don't get deleted, i.e, SQL 'DELETE...', nor is the overridden destroy-method called).

How do I trigger the destruction of the child objects.

Thanks!


回答1:


You need to trigger the destroy callback.

def destroy
  self.is_deleted = true
  self.save
  run_callbacks :destroy
  freeze
end


来源:https://stackoverflow.com/questions/13157987/triggering-dependent-destroy-with-overridden-destroy-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!