Rails 3 Override Destroy without Canceling callbacks, and triggering a ROLLBACK

佐手、 提交于 2019-12-25 08:35:41

问题


My desired behavior is that when destroy is called on an instance, that instance will not actually be destroyed, but it will just be marked as having been destroyed. This needs to percolate up for any model associations.

In the models that I don't want to actually destroy, but just mark them as deleted I have a deactivated field.

From what I can tell the < v3.0.0 way of doing this was to override destroy_without_callbacks (That's the way ActsAsParanoid does it), however that method no longer exists in 3.0.0, plus I'm not excited about overriding private methods.

I also tried implementing callbacks, but per the docs any changes made in the call backs are part of the transaction, and are thus rolled-back as well.

My callback looks like:

  after_destroy :mark_deactivated

  def mark_deactivated
    if self.respond_to?(:deactivated) then
      self.deactivated = DateTime.now
      self.save
      false
    else
      true
    end
  end

How can I prevent the actual destruction of my record without stopping callback, and having my changes rolled-back?


回答1:


Check out this gem: https://github.com/wireframe/delete_paranoid



来源:https://stackoverflow.com/questions/4060131/rails-3-override-destroy-without-canceling-callbacks-and-triggering-a-rollback

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