Disabling callbacks in an ActiveRecord data migration
January 29th, 2009
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”
Sorry, comments are closed for this article.



January 30th, 2009 at 10:13 AM Nice! Very handy in some situations.