What is the order of ActiveRecord callbacks and validations?

Ruby on-RailsRuby on-Rails-3ValidationActiverecord

Ruby on-Rails Problem Overview


I was wondering in what order are callbacks and validations called when an ActiveRecord object is created.

Let’s say I have some custom validations & callbacks like the following:

validates :reference_code, :if => :reference_code, :on => :create
before_create :assign_reference

which one will run first? The callback needs to happen first or else the validation may fail.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

The most-up-to-date version of this list for the latest version of Rails can be found in the ActiveRecord::Callbacks documentation. The lists for Rails 4, 3 & 2 are below.

Rails 4

The most up-to-date version of this list can be found in the Rails 4 Guides.

Creating an object
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback
Updating an object
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback
Destroying an object
  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback

Rails 3

The most up-to-date version of this list can be found in the Rails 3 Guides.

Creating an object
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
Updating an object
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
Destroying an object
  • before_destroy
  • around_destroy
  • after_destroy

Rails 2

The most up-to-date version of this list can be found in the Rails 2.3 Guides

Creating an object
  • before_validation
  • before_validation_on_create
  • after_validation
  • after_validation_on_create
  • before_save
  • before_create
  • INSERT operation
  • after_create
  • after_save
Updating an object
  • before_validation
  • before_validation_on_update
  • after_validation
  • after_validation_on_update
  • before_save
  • before_update
  • UPDATE operation
  • after_update
  • after_save
Destroying an object
  • before_destroy
  • DELETE operation
  • after_destroy

Since you need to first validate the reference_code, the assign_reference method can be called in the after_validation callback or any callback appearing after it in the list I provided above.

Solution 2 - Ruby on-Rails

Rails 5

Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:

1 Creating an Object

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback

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 Destroying an Object

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback

after_save runs both on create and update, but always after the more specific callbacks after_create and after_update, no matter the order in which the macro calls were executed.

before_destroy callbacks should be placed before dependent: :destroy associations (or use the prepend: true option), to ensure they execute before the records are deleted by dependent: :destroy.

Solution 3 - Ruby on-Rails

In addition to knowing the order each type of callback is called relative to the others, it's also important to be aware of the execution order of multiple callbacks of the same type. before_* hooks are executed in the order in which you define them, but after_* hooks are executed in reverse order. This can be surprising if you don't know it.

https://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#module-ActiveRecord::Callbacks-label-Ordering+callbacks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbidView Question on Stackoverflow
Solution 1 - Ruby on-RailsBart JedrochaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDeepsystmView Answer on Stackoverflow
Solution 3 - Ruby on-RailsNalibaView Answer on Stackoverflow