Understanding after_update callback in Rails 4

I have a Rails object with an after_update that sends a record to the queue. And the problem is that I sometimes noticed that the queue is processed faster than the object is actually being updated.

My question is: after_update called not after the update of the object is complete, but when did it start? What callback do I need if I want to do something with it, only a successful update after ?

+5
source share
2 answers

after_save , after_create , after_update are called inside the transaction block, so they will be executed before the SQL statement is executed.

If you want to do something when the statement completes, you must use the after_commit callback.

+11
source

If you consult the Rails documentation, you will find many callbacks that you can use. Best for this job might be "after_commit":

This is straight from Rails Docs (link below)

 3.1 Creating an Object before_validation after_validation before_save around_save before_create around_create after_create after_save after_commit/after_rollback 3.2 Updating an Object before_validation after_validation before_save around_save before_update around_update after_update after_save after_commit/after_rollback 3.3 Destroying an Object before_destroy around_destroy after_destroy 

Rails DOcs: http://guides.rubyonrails.org/active_record_callbacks.html

+2
source

Source: https://habr.com/ru/post/1266523/


All Articles