Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after_create and after_save order

I have done some tests, and I have find out that after_create is called before than after_save (in Rails 2.3.9)

Is that correct ?

thanks

like image 622
Alessandro De Simone Avatar asked May 12 '11 18:05

Alessandro De Simone


People also ask

What is the difference between After_save and After_commit?

There is a slight but important difference between after_save and after_commit callbacks. after_commit is invoked when a database transaction reaches committed state. In case a transaction fails, for instance, while creating a record, after_commit is not invoked by Active Record, but after_save will have run by then.

What is the sequence of callbacks in Rails?

The callback order is as following:after_validation. after_validation_on_create / after_validation_on_update. before_save. before_create.

What does After_commit do in Rails?

When a transaction completes, the after_commit or after_rollback callbacks are called for all models created, updated, or destroyed within that transaction.

What is around save?

around_save. Called before a new or existing object is saved until yield is invoked within the method triggered by the callback. Calling yield causes the object to be saved and then any proceeding code in the method will execute.


1 Answers

Yes, here's the order:

# (1) before_validation
# (2) before_validation_on_create
# (3) after_validation
# (4) after_validation_on_create
# (5) before_save
# (6) before_create
# (7) after_create
# (8) after_save 

Found here:

http://ar.rubyonrails.org/classes/ActiveRecord/Callbacks.html

like image 131
twmills Avatar answered Sep 22 '22 09:09

twmills