Active Record Callbacks are very effective and quite useful in Ruby on Rails. Equally, they can be the allusive and invisible sources of bugs.
In this blog, we will be looking at the basics of ActiveRecord Callbacks.
What is a callback?
It is a method that get called at certain moment of an object’s life cycle.
What is an object life cycle?
When we do a normal operation in the rails application, objects may be created, updated, and destroyed. These life cycle actions are called as object life cycle.
An Active Record provides hooks (called callbacks) into this object life cycle so that you can control your application and its data.
What does the callbacks do?
Callback is basically just a way to trigger logic before, after or around a change to the object’s state. This allows you to execute any arbitrary code automatically whenever any event occurs.
By using the callbacks, We can write the code that will run whenever an Active Record Object is Initialising, Loading, Validating, Saving, Creating, Updating, and Deleting from the database.
To use the available callbacks, you need to register them as callbacks. So There are two different ways in callbacks registration,
First, you can define a method to be called,
class TestCallback < ApplicationRecord # before_validation is one of the macro-style class method before_validation :check_before_validation private # We registered the below ordinary method as callback by using macro-style class method def check_before_validation puts 'Inside before validations.' end end
Second, you can use the macro style. It also receive a block and use this style if the code inside the block is so short and that fits in a single line.
class TestCallback < ApplicationRecord # The before_validation(macro-style class method) is registered as a block. before_validation do puts 'Inside before validations.' end end
We can also be register the callbacks to fire on certain life cycle events. For example, below code will register the callback method check_before_validation to run only on creating a new record.
class TestCallback < ApplicationRecord # The below callback is only fire on "create" life cycle event before_validation :check_before_validation, on: :create private def check_before_validation puts 'Inside before validations.' end end
It is a good practice to declare the callback methods as protected or private. If kept public, It can be called from outside of the model and violate the principle of object encapsulation. See https://www.rubyguides.com/2018/10/method-visibility/ to learn more about method visibility.
Available Callbacks & Order of Operations
Below are the list of all available Active Record Callbacks listed in the order in which they will get executed during the respective object life cycle.
Creating an Object
Updating an Object
Destroying an Object
Finding an Object
Initializing an Object
Touching an Object
The following ActiveRecord methods trigger callbacks to run,
Creating a Record
- save(validate: false)
Updating a Record
- save(validate: false)
- before_validation & after_validation can be skipped with object.save(validate: false).
- The after_initialize callback is triggered every time a new object is initialized.
The following ActiveRecord methods will skip any callbacks defined in the model from running.
These methods should be used with caution, bypassing them without understanding the potential implications may lead to invalid data.
We will see in more detail about all the available callbacks in the upcoming blog.
-Rajesh Kanna R,
Great content! Super high-quality! Keep it up! 🙂