I'm trying to upgrade a Ruby 1.9.3 app to 2.0, and everything seems to have gone smoothly except for one hiccup. I wrote a module which I include in my models to override activerecord destroy. It aliases the existing destroy
method to destroy!
and then overrides destroy
to change a deleted_at
timestamp on the record. Only when I upgrade to ruby 2.0 destroy!
no longer destroys the record, but behaves just like my new override method. Any idea why this would be? The more relevant section of code is below. Full gist here.
def self.included(base)
base.class_eval do
alias_method :destroy!, :destroy
alias_method :delete!, :delete
default_scope -> { where(:deleted_at => nil) }
end
base.send :extend, ClassMethods
base.send :include, InstanceMethods
end
Check out the paranoia gem. It's a Rails 3/4 compatible implementation of soft deletes that does just what you're after. If all you want to do is provide a soft-delete then I'd use the gem and be done with it. If you want to implement soft deletes yourself, then the implementation can give you some insights into how it's been done before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With