ActiveRecord Callbacks List ActiveRecord Callbacks List ruby-on-rails ruby-on-rails

ActiveRecord Callbacks List


You can call Model._save_callbacks to get a list of all callbacks on save. You can then filter it down to what kind you need e.g. :before or :after like this:

Model._save_callbacks.select {|cb| cb.kind == :before}

Works the same for Model._destroy_callbacks etc.


The docs say "There are nineteen callbacks in total"... but they don't seem to say what all of those 19 actually are!

For those who Googled "list of all ActiveRecord callbacks" like I did, here's the list (found by using ActiveRecord::Callbacks::CALLBACKS as described in the question):

:after_initialize:after_find:after_touch:before_validation:after_validation:before_save:around_save:after_save:before_create:around_create:after_create:before_update:around_update:after_update:before_destroy:around_destroy:after_destroy:after_commit:after_rollback


Note that if you simply want to trigger callbacks, you can use the #run_callbacks(kind) method:

o = Order.find 123 # Created with SQLo.run_callbacks(:create)o.run_callbacks(:save)o.run_callbacks(:commit)