Active Record Query Interface and Associations in Rails — Ruby Deep Dive[14]

Bhavyansh @ DiversePixel
3 min readJul 31, 2024

--

Active Record is the ORM layer Rails uses to interact with databases, allowing developers to perform database operations through Ruby methods rather than raw SQL. This article will cover the query interface and various types of associations in Active Record, with practical examples to illustrate their use.

1. Active Record Query Interface

Active Record’s query interface provides methods to interact with your database in a Ruby-friendly way. Here are some key methods and their usage:

1.1 Basic Querying

  • Finding a Single Record by ID
user = User.find(10) # Finds the user with ID 10
  • Finding the First or Last Record
first_user = User.first
last_user = User.last
  • Finding a Record by Attribute
user = User.find_by(email: 'example@example.com')
  • Retrieving Multiple Records
users = User.where(active: true).limit(5)

1.2 Batch Processing

For large datasets, use batch processing to handle records in chunks:

  • Using find_each
User.find_each(batch_size: 1000) do |user|   # Process each user end
  • Using find_in_batches
User.find_in_batches(batch_size: 1000) do |batch|
batch.each do |user|
# Process each user
end
end

1.3 Query Conditions

  • Hash Conditions
Order.where(customer_id: 1, status: 'shipped')
  • LIKE Conditions
Product.where("name LIKE ?", "%Laptop%")
  • NOT Conditions
Customer.where.not(last_name: 'Smith')
  • OR Conditions
Customer.where(first_name: 'John').or(Customer.where(last_name: 'Doe'))

1.4 Ordering and Limiting Records

  • Ordering Records
Product.order(:price)
Product.order(price: :desc)
  • Limit and Offset
Product.limit(10).offset(20) # Useful for pagination

1.5 Grouping and Aggregations

  • Grouping Records
Order.group(:status).count
  • Aggregating Data
Order.group(:customer_id).sum(:total_price)
# The result of this query will be a hash where:
# - The keys are unique customer IDs
# - The values are the sum of total prices for orders associated with each customer

1.6 Other Query Methods

  • Finding or Creating Records
user = User.find_or_create_by(email: 'newuser@example.com')
  • Checking Existence
Product.exists?(id: 1)

2. Active Record Associations

Associations define the relationships between different models, allowing you to query related records efficiently. Here are the most common associations:

2.1 belongs_to Association

The belongs_to association sets up a one-to-one connection where each instance of the declaring model references one instance of another model.

  • Example: An Order and a Customer
class Order < ApplicationRecord
belongs_to :customer
end

class Customer < ApplicationRecord
has_many :orders
end
  • Use Case: Each order belongs to a customer, and a customer can have many orders.

2.2 has_one Association

A has_one association indicates that one model has a reference to another model, creating a one-to-one relationship.

  • Example: A User Profile
class User < ApplicationRecord
has_one :profile
end

class Profile < ApplicationRecord
belongs_to :user
end
  • Use Case: Each user has one profile, and each profile belongs to a user.

2.3 has_many Association

A has_many association indicates a one-to-many relationship where one model has many instances of another model.

  • Example: A Blog and Its Posts
class Blog < ApplicationRecord
has_many :posts
end

class Post < ApplicationRecord
belongs_to :blog
end
  • Use Case: A blog can have many posts, and each post belongs to a specific blog.

2.4 has_many :through Association

The has_many :through association is used to set up a many-to-many relationship with a join model.

  • Example: Students and Courses through Enrollments
class Student < ApplicationRecord
has_many :enrollments
has_many :courses, through: :enrollments
end

class Course < ApplicationRecord
has_many :enrollments
has_many :students, through: :enrollments
end

class Enrollment < ApplicationRecord
belongs_to :student
belongs_to :course
end
  • Use Case: Students can enroll in many courses, and each course can have many students enrolled.

2.5 has_one :through Association

The has_one :through association sets up a one-to-one connection through a join model.

  • Example: Users and Their Latest Orders through OrderDetails
class User < ApplicationRecord
has_one :latest_order, -> { order(created_at: :desc) },
through: :orders, source: :order
end

class Order < ApplicationRecord
belongs_to :user
has_one :order_detail
end

class OrderDetail < ApplicationRecord
belongs_to :order
end
  • Use Case: Users can have one latest order through their order details.

2.6 has_and_belongs_to_many Association

The has_and_belongs_to_many association is used for many-to-many relationships without a join model.

  • Example: Authors and Books
class Author < ApplicationRecord
has_and_belongs_to_many :books
end

class Book < ApplicationRecord
has_and_belongs_to_many :authors
end
  • Use Case: An author can write many books, and a book can be written by many authors.

Conclusion

Understanding and leveraging Active Record’s query interface and associations effectively can greatly enhance the efficiency and readability of your Rails applications. By using these features, you can build robust, data-driven applications with less boilerplate code and better maintainability.

--

--

Bhavyansh @ DiversePixel
Bhavyansh @ DiversePixel

Written by Bhavyansh @ DiversePixel

Hey I write about Tech. Join me as I share my tech learnings and insights. 🚀

No responses yet