123456789_123456789_123456789_123456789_123456789_

DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.

Active Record Callbacks

This guide teaches you how to hook into the life cycle of your Active Record objects.

After reading this guide, you will know:


The Object Life Cycle

During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks 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.

class Baby < ApplicationRecord
  after_create -> { puts "Congratulations!" }
end
irb> @baby = Baby.create
Congratulations!

As you will see, there are many life cycle events and you can choose to hook into any of these either before, after, or even around them.

Callbacks Overview

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.

Callback Registration

In order to use the available callbacks, you need to register them. You can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks:

class User < ApplicationRecord
  validates :, :email, presence: true

  before_validation :

  private
    def 
      if .blank?
        self. = email unless email.blank?
      end
    end
end

The macro-style class methods can also receive a block. Consider using this style if the code inside your block is so short that it fits in a single line:

class User < ApplicationRecord
  validates :, :email, presence: true

  before_create do
    self.name = .capitalize if name.blank?
  end
end

Alternatively you can pass a proc to the callback to be triggered.

class User < ApplicationRecord
  before_create ->(user) { user.name = user..capitalize if user.name.blank? }
end

Lastly, you can define your own custom callback object, which we will cover later in more detail below.

class User < ApplicationRecord
  before_create MaybeAddName
end

class MaybeAddName
  def self.before_create(record)
    if record.name.blank?
      record.name = record..capitalize
    end
  end
end

Callbacks can also be registered to only fire on certain life cycle events, this allows complete control over when and in what context your callbacks are triggered.

class User < ApplicationRecord
  before_validation :normalize_name, on: :create

  # :on takes an array as well
  after_validation :set_location, on: [ :create, :update ]

  private
    def normalize_name
      self.name = name.downcase.titleize
    end

    def set_location
      self.location = LocationService.query(self)
    end
end

It is considered good practice to declare callback methods as private. If left public, they can be called from outside of the model and violate the principle of object encapsulation.

WARNING. Avoid calls to update, save or other methods which create side-effects to the object inside your callback. For example, don't call update(attribute: "value") within a callback. This can alter the state of the model and may result in unexpected side effects during commit. Instead, you can safely assign values directly (for example, self.attribute = "value") in before_create / before_update or earlier callbacks.

Available Callbacks

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:

Creating an Object

Updating an Object

WARNING. 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.

Destroying an Object

NOTE: 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.

WARNING. after_commit makes very different guarantees than after_save, after_update, and after_destroy. For example if an exception occurs in an after_save the transaction will be rolled back and the data will not be persisted. While anything that happens after_commit can guarantee the transaction has already completed and the data was persisted to the database. More on transactional callbacks below.

after_initialize and after_find

Whenever an Active Record object is instantiated the after_initialize callback will be called, either by directly using new or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record initialize method.

When loading a record from the database the after_find callback will be called. after_find is called before after_initialize if both are defined.

NOTE: The after_initialize and after_find callbacks have no before_* counterparts.

They can be registered just like the other Active Record callbacks.

class User < ApplicationRecord
  after_initialize do |user|
    puts "You have initialized an object!"
  end

  after_find do |user|
    puts "You have found an object!"
  end
end
irb> User.new
You have initialized an object!
=> #<User id: nil>

irb> User.first
You have found an object!
You have initialized an object!
=> #<User id: 1>

after_touch

The after_touch callback will be called whenever an Active Record object is touched.

class User < ApplicationRecord
  after_touch do |user|
    puts "You have touched an object"
  end
end
irb> u = User.create(name: 'Kuldeep')
=> #<User id: 1, name: "Kuldeep", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49">

irb> u.touch
You have touched an object
=> true

It can be used along with belongs_to:

class Book < ApplicationRecord
  belongs_to :library, touch: true
  after_touch do
    puts 'A Book was touched'
  end
end

class Library < ApplicationRecord
  has_many :books
  after_touch :log_when_books_or_library_touched

  private
    def log_when_books_or_library_touched
      puts 'Book/Library was touched'
    end
end
irb> @book = Book.last
=> #<Book id: 1, library_id: 1, created_at: "2013-11-25 17:04:22", updated_at: "2013-11-25 17:05:05">

irb> @book.touch # triggers @book.library.touch
A Book was touched
Book/Library was touched
=> true

Running Callbacks

The following methods trigger callbacks:

Additionally, the after_find callback is triggered by the following finder methods:

The after_initialize callback is triggered every time a new object of the class is initialized.

NOTE: The find_by_* and find_by_*! methods are dynamic finders generated automatically for every attribute. Learn more about them at the Dynamic finders section

Skipping Callbacks

Just as with validations, it is also possible to skip callbacks by using the following methods:

These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data. Refer to the method documentation to learn more.

Halting Execution

As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks, and the database operation to be executed.

The whole callback chain is wrapped in a transaction. If any callback raises an exception, the execution chain gets halted and a ROLLBACK is issued. To intentionally stop a chain use:

throw :abort

WARNING. Any exception that is not ::ActiveRecord::Rollback or ::ActiveRecord::RecordInvalid will be re-raised by Rails after the callback chain is halted. Additionally, may break code that does not expect methods like save and update (which normally try to return true or false) to raise an exception.

NOTE: If an ::ActiveRecord::RecordNotDestroyed is raised within after_destroy, before_destroy or around_destroy callback, it will not be re-raised and the destroy method will return false.

Relational Callbacks

Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many articles. A user's articles should be destroyed if the user is destroyed. Let's add an after_destroy callback to the User model by way of its relationship to the Article model:

class User < ApplicationRecord
  has_many :articles, dependent: :destroy
end

class Article < ApplicationRecord
  after_destroy :log_destroy_action

  def log_destroy_action
    puts 'Article destroyed'
  end
end
irb> user = User.first
=> #<User id: 1>
irb> user.articles.create!
=> #<Article id: 1, user_id: 1>
irb> user.destroy
Article destroyed
=> #<User id: 1>

Association Callbacks

Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:

You define association callbacks by adding options to the association declaration. For example:

class Author < ApplicationRecord
  has_many :books, before_add: :check_credit_limit

  def check_credit_limit(book)
    # ...
  end
end

Rails passes the object being added or removed to the callback.

You can stack callbacks on a single event by passing them as an array:

class Author < ApplicationRecord
  has_many :books,
    before_add: [:check_credit_limit, :calculate_shipping_charges]

  def check_credit_limit(book)
    # ...
  end

  def calculate_shipping_charges(book)
    # ...
  end
end

If a before_add callback throws :abort, the object does not get added to the collection. Similarly, if a before_remove callback throws :abort, the object does not get removed from the collection:

# book won't be added if the limit has been reached
def check_credit_limit(book)
  throw(:abort) if limit_reached?
end

NOTE: These callbacks are called only when the associated objects are added or removed through the association collection:

# Triggers `before_add` callback
author.books << book
author.books = [book, book2]

# Does not trigger the `before_add` callback
book.update(author_id: 1)

Conditional Callbacks

As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the :if and :unless options, which can take a symbol, a Proc or an Array.

You may use the :if option when you want to specify under which conditions the callback should be called. If you want to specify the conditions under which the callback should not be called, then you may use the :unless option.

Using :if and :unless with a Symbol

You can associate the :if and :unless options with a symbol corresponding to the name of a predicate method that will get called right before the callback.

When using the :if option, the callback won't be executed if the predicate method returns false; when using the :unless option, the callback won't be executed if the predicate method returns true. This is the most common option.

class Order < ApplicationRecord
  before_save :normalize_card_number, if: :paid_with_card?
end

Using this form of registration it is also possible to register several different predicates that should be called to check if the callback should be executed. We will cover this below.

Using :if and :unless with a Proc

It is possible to associate :if and :unless with a Proc object. This option is best suited when writing short validation methods, usually one-liners:

class Order < ApplicationRecord
  before_save :normalize_card_number,
    if: Proc.new { |order| order.paid_with_card? }
end

As the proc is evaluated in the context of the object, it is also possible to write this as:

class Order < ApplicationRecord
  before_save :normalize_card_number, if: Proc.new { paid_with_card? }
end

Multiple Callback Conditions

The :if and :unless options also accept an array of procs or method names as symbols:

class Comment < ApplicationRecord
  before_save :filter_content,
    if: [:subject_to_parental_control?, :untrusted_author?]
end

You can easily include a proc in the list of conditions:

class Comment < ApplicationRecord
  before_save :filter_content,
    if: [:subject_to_parental_control?, Proc.new { untrusted_author? }]
end

Using Both :if and :unless

Callbacks can mix both :if and :unless in the same declaration:

class Comment < ApplicationRecord
  before_save :filter_content,
    if: Proc.new { forum.parental_control? },
    unless: Proc.new { author.trusted? }
end

The callback only runs when all the :if conditions and none of the :unless conditions are evaluated to true.

Callback Classes

Sometimes the callback methods that you'll write will be useful enough to be reused by other models. Active Record makes it possible to create classes that encapsulate the callback methods, so they can be reused.

Here's an example where we create a class with an after_destroy callback to deal with the clean up of discarded files on the filesystem. This behavior may not be unique to our PictureFile model and we may want to share it, so it's a good idea to encapsulate this into a separate class. This will make testing that behavior and changing it much easier.

class FileDestroyerCallback
  def after_destroy(file)
    if File.exist?(file.filepath)
      File.delete(file.filepath)
    end
  end
end

When declared inside a class, as above, the callback methods will receive the model object as a parameter. This will work on any model that uses the class like so:

class PictureFile < ApplicationRecord
  after_destroy FileDestroyerCallback.new
end

Note that we needed to instantiate a new FileDestroyerCallback object, since we declared our callback as an instance method. This is particularly useful if the callbacks make use of the state of the instantiated object. Often, however, it will make more sense to declare the callbacks as class methods:

class FileDestroyerCallback
  def self.after_destroy(file)
    if File.exist?(file.filepath)
      File.delete(file.filepath)
    end
  end
end

When the callback method is declared this way, it won't be necessary to instantiate a new FileDestroyerCallback object in our model.

class PictureFile < ApplicationRecord
  after_destroy FileDestroyerCallback
end

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

Transaction Callbacks

after_commit and after_rollback

There are two additional callbacks that are triggered by the completion of a database transaction: after_commit and after_rollback. These callbacks are very similar to the after_save callback except that they don't execute until after database changes have either been committed or rolled back. They are most useful when your Active Record models need to interact with external systems which are not part of the database transaction.

Consider, for example, the previous example where the PictureFile model needs to delete a file after the corresponding record is destroyed. If anything raises an exception after the after_destroy callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state. For example, suppose that picture_file_2 in the code below is not valid and the save! method raises an error.

PictureFile.transaction do
  picture_file_1.destroy
  picture_file_2.save!
end

By using the after_commit callback we can account for this case.

class PictureFile < ApplicationRecord
  after_commit :delete_picture_file_from_disk, on: :destroy

  def delete_picture_file_from_disk
    if File.exist?(filepath)
      File.delete(filepath)
    end
  end
end

NOTE: The :on option specifies when a callback will be fired. If you don't supply the :on option the callback will fire for every action.

WARNING. When a transaction completes, the after_commit or after_rollback callbacks are called for all models created, updated, or destroyed within that transaction. However, if an exception is raised within one of these callbacks, the exception will bubble up and any remaining after_commit or after_rollback methods will not be executed. As such, if your callback code could raise an exception, you'll need to rescue it and handle it within the callback in order to allow other callbacks to run.

WARNING. The code executed within after_commit or after_rollback callbacks is itself not enclosed within a transaction.

WARNING. In the context of a single transaction, if you interact with multiple loaded objects that represent the same record in the database, there's a crucial behavior in the after_commit and after_rollback callbacks to note. These callbacks are triggered only for the first object of the specific record that undergoes a change within the transaction. Other loaded objects, despite representing the same database record, will not have their respective after_commit or after_rollback callbacks triggered. This nuanced behavior is particularly impactful in scenarios where you expect independent callback execution for each object associated with the same database record. It can influence the flow and predictability of callback sequences, leading to potential inconsistencies in application logic following the transaction.

Aliases for after_commit

Since using the after_commit callback only on create, update, or delete is common, there are aliases for those operations:

class PictureFile < ApplicationRecord
  after_destroy_commit :delete_picture_file_from_disk

  def delete_picture_file_from_disk
    if File.exist?(filepath)
      File.delete(filepath)
    end
  end
end

WARNING. Using both after_create_commit and after_update_commit with the same method name will only allow the last callback defined to take effect, as they both internally alias to after_commit which overrides previously defined callbacks with the same method name.

class User < ApplicationRecord
  after_create_commit :log_user_saved_to_db
  after_update_commit :log_user_saved_to_db

  private
    def log_user_saved_to_db
      puts 'User was saved to database'
    end
end
irb> @user = User.create # prints nothing

irb> @user.save # updating @user
User was saved to database

after_save_commit

There is also after_save_commit, which is an alias for using the after_commit callback for both create and update together:

class User < ApplicationRecord
  after_save_commit :log_user_saved_to_db

  private
    def log_user_saved_to_db
      puts 'User was saved to database'
    end
end
irb> @user = User.create # creating a User
User was saved to database

irb> @user.save # updating @user
User was saved to database

Transactional Callback Ordering

By default, callbacks will run in the order they are defined. However, when defining multiple transactional after_ callbacks (after_commit, after_rollback, etc), the order could be reversed from when they are defined.

class User < ActiveRecord::Base
  after_commit { puts("this actually gets called second") }
  after_commit { puts("this actually gets called first") }
end

NOTE: This applies to all after_*_commit variations too, such as after_destroy_commit.

This order can be set via configuration:

config.active_record.run_after_transaction_callbacks_in_order_defined = false

When set to true (the default from Rails 7.1), callbacks are executed in the order they are defined. When set to false, the order is reversed, just like in the example above.