I have around 40 models in my RoR application. I want to setup a after_save callback for all models. One way is to add it to all models. Since this callback has the same code to run, is there a way to define it globally once so that it gets invoked for all models.
I tried this with no luck:
class ActiveRecord::Base
after_save :do_something
def do_something
# ....
end
end
Same code works if I do it in individual models.
Thanks,
Imran
You should use observers for this:
In order to activate an observer, list it in the config.active_record.observers configuration setting in your config/application.rb file.
Note
In Rails 4, the observer feature is removed from core. Use the https://github.com/rails/rails-observers gem.