问题
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