ruby on rails - What's the best way to conditionally override ActiveRecord's deletion mechanism? -
i trying hijack rails' deletion mechanism make behave differently set of models.
both activerecord::base#delete , #destroy lead activerecord::relation#delete_all, make sense override method.
i have tried...
class mymodel < activerecord::base class << def delete_all "my destruction mechanism" end end end ... ::all method returns different object every time...
class mymodel < activerecord::base def self.all super.tap |obj| class << obj def delete_all "my destruction mechanism" end end end end end ... ::all isn't only scope needs overridden anyway...
class activerecord::relation def delete_all(*args) "my destruction mechanism" end end ... can only apply mymodel , subclasses...
class activerecord::relation def delete_all(*args) if @klass.new.is_a?(mymodel) "my destruction mechanism" else super end end end ... causes stack overflows on other models.
help?
overriding delete , destroy on model should accomplish of want. check out how paranoia gem accomplishes overriding. library's 200 lines or so, , handles associated models (eg when have dependent: :destroy).
Comments
Post a Comment