Ruby on Rails - Callbacks in Rails
Introduction
Callbacks in Rails are methods that get called at certain points of an object's lifecycle. They allow you to add behavior to your models that runs at certain stages of creating, updating, and destroying an object. This guide will cover how to use callbacks in Rails models effectively.
Key Points:
- Callbacks are used to perform actions at specific points in the lifecycle of an object.
- Rails provides several built-in callbacks for common actions like saving, creating, updating, and destroying objects.
- Understanding how to use callbacks can help you manage the state and behavior of your models more effectively.
Types of Callbacks
Rails provides a variety of callbacks that you can use in your models. These include:
- before_validation and after_validation: Runs before and after the validation of an object.
- before_save and after_save: Runs before and after saving an object.
- before_create and after_create: Runs before and after creating an object.
- before_update and after_update: Runs before and after updating an object.
- before_destroy and after_destroy: Runs before and after destroying an object.
Using Callbacks
To use a callback in your model, you can define a method and use a callback macro to register it. Here is an example:
# app/models/article.rb
class Article < ApplicationRecord
before_save :capitalize_title
private
def capitalize_title
self.title = title.capitalize
end
end
In this example, the capitalize_title
method is called before saving the Article
object.
Conditional Callbacks
You can also specify conditions for callbacks to control when they should run. Here is an example using the :if
and :unless
options:
# app/models/article.rb
class Article < ApplicationRecord
before_save :capitalize_title, if: :title_present?
private
def capitalize_title
self.title = title.capitalize
end
def title_present?
title.present?
end
end
In this example, the capitalize_title
method is only called if the title_present?
method returns true.
Halting Execution
Callbacks can halt the execution of the callback chain by throwing :abort
. Here is an example:
# app/models/article.rb
class Article < ApplicationRecord
before_save :check_title
private
def check_title
if title.blank?
errors.add(:title, "can't be blank")
throw :abort
end
end
end
In this example, the check_title
method halts the save operation if the title is blank.
Common Callbacks
Here are some common callbacks and their typical use cases:
- before_validation: Modify attributes before validation.
- after_validation: Perform actions after validation, such as logging.
- before_save: Prepare data before saving, like normalizing text fields.
- after_save: Perform actions after saving, like clearing caches.
- before_create: Set defaults for new records.
- after_create: Send notifications after a record is created.
- before_update: Track changes before updating a record.
- after_update: Trigger events after updating a record.
- before_destroy: Check dependencies before deleting a record.
- after_destroy: Clean up associated records after deletion.
Conclusion
Callbacks are a powerful feature in Rails that allow you to add custom behavior to your models at various points in their lifecycle. By understanding and using callbacks effectively, you can manage the state and behavior of your models more efficiently.