Callbacks
CQL's Active Record provides lifecycle callbacks that allow you to trigger logic at various points during an object's life. This is useful for tasks like data normalization before validation, encrypting passwords before saving, or sending notifications after an action.
The CQL::ActiveRecord::Callbacks
module is automatically included when you use CQL::ActiveRecord::Model(Pk)
.
Available Callbacks
Callbacks are methods defined in your model that are registered to be called at specific moments. CQL supports the following callbacks:
Validation Callbacks:
before_validation(method_name)
: Called beforevalidate
is run.after_validation(method_name)
: Called aftervalidate
completes.
Save Callbacks (run for both create and update):
before_save(method_name)
: Called before the record is saved to the database.after_save(method_name)
: Called after the record is saved to the database.
Create Callbacks (run only when a new record is saved):
before_create(method_name)
: Called before a new record is inserted into the database.after_create(method_name)
: Called after a new record is inserted into the database.
Update Callbacks (run only when an existing record is saved):
before_update(method_name)
: Called before an existing record is updated in the database.after_update(method_name)
: Called after an existing record is updated in the database.
Destroy Callbacks:
before_destroy(method_name)
: Called before a record is deleted from the database.after_destroy(method_name)
: Called after a record is deleted from the database.
Registering Callbacks
You register a callback by calling its macro with the name of the method (as a Symbol) to be executed.
In this example:
normalize_email
will run before validations.set_status_if_nil
will run before any save operation (create or update).send_welcome_email
will run only after a new user is created.record_login_time
will run only before an existing user is updated.log_deletion
will run after a user is destroyed.
Halting Execution
If a before_validation
, before_save
, before_create
, before_update
, or before_destroy
callback method returns false
(explicitly false
, not nil
or other falsy values), the callback chain is halted. This means:
Subsequent callbacks of the same type (e.g., other
before_save
methods) will not be executed.The main action (validation, save, create, update, or destroy) will be canceled.
For
save
,create
,update
, it will returnfalse
.For
save!
,create!
,update!
, it will not raiseRecordInvalid
due to validation errors (if any ran), but simply won't proceed with persistence.For
destroy
, it will returnfalse
, and the record will not be deleted.
Example of halting:
after_*
callbacks do not have the power to halt the chain, as the primary action has already completed.
Order of Callbacks
When multiple callbacks are registered for the same event, they are executed in the order they were defined in the model.
CQL aims to follow a similar callback order to other popular ORMs like Rails Active Record during the save
process:
before_validation
Validations are run (
validate
method)after_validation
before_save
If new record:
before_create
If existing record:
before_update
Database operation (INSERT or UPDATE)
If new record:
after_create
If existing record:
after_update
after_save
For destroy
:
before_destroy
Database operation (DELETE)
after_destroy
Use Cases
Data Manipulation: Normalize data (e.g., downcasing emails), set default values, generate tokens.
Lifecycle Management: Update related objects, log changes, manage state transitions.
Notifications: Send emails or push notifications after certain events (e.g.,
after_create
).Conditional Logic: A callback method can contain logic to decide if it should perform an action, or even halt the entire operation.
Callbacks are a powerful tool for adding behavior to your models without cluttering your controller or service logic. However, use them judiciously, as complex callback chains can sometimes make debugging harder.
Last updated
Was this helpful?