What is the anatomy of Active Record in Ruby on Rails?

Active Record is the object-relational mapping (ORM) module of the Ruby on Rails framework. It allows connection with different databases by providing an interface and a set of standardized conventions. An Active Record maps database tables to Ruby classes, allowing developers to perform database operations using Ruby code.

The structure of the Active Record entity encompasses the following elements:

  • Methods: These refer to the available actions that can be executed on the database table. They are implemented as functions within the Ruby class.

  • Attributes: These encompass the fields (columns) within the database table. Instance variables in the Ruby class represent them.

  • Callbacks: These are functions that are triggered before or after a specific operation is carried out on the database table. They are implemented as instance methods in the Ruby class.

The anatomy of Active Record in Ruby on Rails can be explained using an example. Let’s consider a simple application with a User model that represents a user in a system. The corresponding database table is typically named users.

  • Model: Models in Ruby on Rails are represented by Ruby classes that derive from the ActiveRecord::Base class. To define the User model, we make a file called user.rb in the app/models directory and include the necessary code, as shown below:

class User < ActiveRecord::Base
end
  • Database table: Active Record assumes the presence of a table of the user in the database. This table should have columns corresponding to the attributes we intend to store for each user, such as name, email, and password.

  • Associations: Active Record offers a mechanism to establish relationships between models. For instance, if a user has multiple posts, we can define this association within the model User model. This allows us to access a user’s posts using the association, e.g., user.posts.

class User < ActiveRecord::Base
has_many :posts
end

  • Validations: Active Record allows us to define validations for the model's attributes. For instance, to ensure the existence and distinctiveness of the email attribute, we can incorporate the subsequent code within the model User:

class User < ActiveRecord::Base
validates :email, presence: true, uniqueness: true
end
  • CRUD Operations: Active Record provides a range of functions to execute operations involving creating, reading, updating, and deleting within the database.

    • Create: To make a new user, we can utilize the create method provided by Active Record:

user = User.create(name: "Paul", email: "paul@example.com", password: "password")
  • Read: To retrieve users from the database, we can use methods like find, where, and all:

user = User.find(23) # Retrieves the user with ID 23
users = User.where(name: "Paul") # Retrieves users with name john
all_users = User.all # Retrieves all users
  • Update: To update a user’s attributes, we can use the update method:

User.update(name: "Paul son")
  • Delete: To delete a user, we can employ the destroy method:

User.destroy(1) # 1 is the id of the user

Here is an executable sample of an Active Record class:

//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
Demo Rails application to test Active Record

Note: Clicking the “Run” button in the above widget will take you directly to the Rails console. After that you can try different operations e.g.,

User.create(name: "Paul", email: "paul@example.com", password: "password")

Conclusion

These are just a few examples of what we can do with Active Record in Ruby on Rails. Active Record provides a rich set of methods and features for working with databases, making it easier to interact with data using Ruby code.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved