Quantcast

It’s often useful to disable ActiveRecord callbacks such as :after_save when migrating data. It’s rather easy to do:


Foo.after_create.clear
Foo.after_save.clear
... migration code ...

If you only need to disable certain actions, it’s also trivial:


Foo.after_save.reject! {|callback| callback.method.to_s == 'some_method_name' }

This sort of thing should never be used in application code, if you’re doing this then your model is broken. However, it’s great for data migrations.

1 Response to “Disabling callbacks in an ActiveRecord data migration”

  1. Carsten Nielsen Says:
    Nice! Very handy in some situations.

Sorry, comments are closed for this article.