Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of ActiveRecord callbacks and validations?

People also ask

What are Active Record methods?

Active Record allows you to validate the state of a model before it gets written into the database. There are several methods that you can use to check your models and validate that an attribute value is not empty, is unique and not already in the database, follows a specific format, and many more.

How many callbacks are there in Rails?

9 Callback Classes You can declare as many callbacks as you want inside your callback classes.

What are callbacks in Ruby?

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.

What does Active Record base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.


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.


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.