Understanding Callbacks, Join Tables, and Manual Testing in Rails
Introduction to Callbacks
A callback is a method called at specific moments of an object’s lifecycle (right before or after creation, deletion, update, validation, saving, or loading from the database).
example:
class Listing < ApplicationRecord
after_create :set_expiry_date
private
def set_expiry_date
expiry_date = Date.today + 30.days
self.update_column(:expires_on, expiry_date)
end
end
Here, we set an expiry date for a listing 30 days after its creation. The available callback methods and their order of execution are as follows:
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
It’s important to note that `after_save` runs both on create and update but follows more specific callbacks, like `after_create` and `after_update`, regardless of the order of macro calls.
Creating a Join Table using Migrations
Join tables are essential for establishing many-to-many relationships between models. Let’s assume you have two models, Tags, and Projects, and you want to associate them using a has_and_belongs_to_many relation. To do this, you’ll need a join table:
class CreateProjectsTagsJoinTableMigration < ActiveRecord::Migration
def change
create_table :projects_tags, id: false do |t|
t.integer :project_id
t.integer :tag_id
end
end
end
Follow the convention that the model name which alphabetically precedes the other should come first in the table name. In this case, “Project” comes before “Tag,” so the table is named “projects_tags.” Additionally, since this table’s primary purpose is facilitating associations, you can skip the IDs by specifying `id: false`. Remember that the table name should be a compound plural form of the individual models, while column names should be singular.
Manually Testing Your Models
Testing Active Record models through your command line interface is straightforward. Follow these steps:
1. Navigate to your app directory in your terminal.
2. Start the Rails console by entering `rails console`.
You can perform Active Record operations on your database in the console. For instance, if you have a Users table with name and email columns, you can create a new user and find existing ones:
User.create(name: “John”, email: “john@example.com”)
User.find_by(email: “john@example.com”)
User.first
To update a record using a model instance:
user = User.find(1)
user.update(first_name: “bhartee”, last_name: “sahare”)
The `update` method attempts to update the specified attributes in a single transaction, returning true for success and false for failure.
In summary, understanding callbacks, creating join tables, and manually testing your models are fundamental aspects of Ruby on Rails development that enable you to build robust and flexible applications.
Happy Callback in Ruby on Rails! 🚀 Please follow for more updates BharteeTechRoR