I recently found that multiple after_commit
s defined in the same model get called in reverse order. For example
after_commit method1, :on => :create
after_commit method2, :on => :create
method2
gets called before method1
.
Is it always called in FILO order?
The callback order is as following:after_validation_on_create / after_validation_on_update. before_save. before_create.
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks (called callbacks) into this object life cycle so that you can control your application and its data. Callbacks allow you to trigger logic before or after an alteration of an object's state.
after_commit is a type of active record callback.
This behaviour is still present in Rails 5.2.2.1.
My solution:
after_commit :after_commit_callbacks, :on => :create
def after_commit_callbacks
method1
method2
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With