Understanding Active Record and Database Query Commands in Ruby on Rails

Ashvin Choudhary
3 min readJun 22, 2023

--

Ruby on Rails is a powerful web development framework that follows the Model-View-Controller (MVC) architectural pattern. One of the key components of Rails is Active Record, an Object-Relational Mapping (ORM) library that simplifies database interactions. Active Record provides a straightforward way to perform database queries and manipulate data using Ruby code, eliminating the need for writing complex SQL queries. In this blog post, we will explore what Active Record is and delve into various Active Record query commands in Ruby on Rails.

What is Active Record?
Active Record is an ORM framework that maps database tables to Ruby classes and objects. It enables developers to interact with the database using object-oriented techniques, making database operations more intuitive and less error-prone. Active Record not only handles database connectivity but also includes a rich set of features for querying, associations, validations, and more.

Database Query Commands using Active Record:
1. Retrieving Records:
— `Model.all`: Returns all records from the corresponding table.
— `Model.find(id)`: Finds a record with the specified ID.
— `Model.find_by(column_name: value)`: Finds the first record that matches the given conditions.
— `Model.where(column_name: value)`: Retrieves records that satisfy the specified conditions.
— `Model.order(column_name: :asc)`: Retrieves records sorted in ascending order based on the specified column.

2. Creating and Updating Records:
— `Model.new(attributes)`: Creates a new instance of the model with the specified attributes.
— `Model.create(attributes)`: Creates a new record in the database with the specified attributes.
— `record.save`: Saves the changes made to an existing record.
— `record.update(attributes)`: Updates the attributes of an existing record.

3. Deleting Records:
— `record.destroy`: Deletes a specific record.
— `Model.destroy_all`: Deletes all records from the corresponding table.
— `Model.delete_all`: Deletes all records from the corresponding table without triggering callbacks.

4. Associations:
— `belongs_to`: Establishes a one-to-one association with another model.
— `has_many`: Establishes a one-to-many association with another model.
— `has_one`: Establishes a one-to-one association with another model.
— `has_and_belongs_to_many`: Establishes a many-to-many association with another model.

5. Querying with Conditions:
— `Model.where(column_name: value)`: Retrieves records that match the specified conditions.
— `Model.where.not(column_name: value)`: Retrieves records that do not match the specified conditions.
— `Model.where(“column_name LIKE ?”, “%value%”)`: Retrieves records based on partial matching.
— `Model.where(column_name: value1, column2_name: value2)`: Retrieves records based on multiple conditions.

Active Record plays a crucial role in simplifying database interactions in Ruby on Rails applications. It provides a higher level of abstraction by allowing developers to perform database queries and manipulations using Ruby code, reducing the need for writing complex SQL queries. In this blog post, we covered some of the essential Active Record query commands for retrieving, creating, updating, and deleting records. Additionally, we explored associations and querying with conditions. With Active Record, developers can focus more on the application logic and worry less about the intricacies of database operations.

--

--

Ashvin Choudhary

Ashvin Choudhary is an AWS Certified Senior Software Engineer with 8+ years of experience. He's enthusiastic about tech and believes in lifelong learning.