Ruby-on-rails – ActiveRecord Callbacks List

activerecordcallbackruby-on-rails

I've been going through the rails source for a while now, and I don't think there's a better way of getting the list of all callbacks other than: ActiveRecord::Callbacks::CALLBACKS – which is a constant list.

Meaning if you're using a gem like devise_invitable that adds a new callback called :invitation_accepted with the score :after and :before then ActiveRecord::Callbacks::CALLBACKS will not work.

Do you know of an easy fix, other than opening up rails modules and making sure there's an internal list of call-backs per model class?

Best Answer

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.