Eager Loading
Eager Loading is a technique in Ruby on Rails that loads associated records in advance to reduce the number of database queries. It helps prevent the N+1 query problem by fetching related data upfront instead of executing additional queries for each associated record.
Table of Contents
What Is Eager Loading in Ruby on Rails?
By default, Rails uses lazy loading for Active Record associations. When an associated record is accessed, Rails executes a separate database query to retrieve it. While this works for individual records, it can result in many unnecessary queries when iterating over collections.
Eager Loading allows Rails to retrieve associated records ahead of time using methods like includes, preload, or eager_load. The most commonly used method is includes, which loads related data efficiently and helps improve application performance.
Why Is Eager Loading Useful?
Without eager loading, an application may execute one query to retrieve a collection of records and then an additional query for each associated record, leading to the N+1 query problem.
Eager Loading helps by:
- Preventing the N+1 query problem
- Reducing the total number of database queries
- Improving page response times
- Lowering database load
- Making applications more scalable when working with associations
It is especially useful for pages that display associated data, such as blog posts with authors, orders with customers, or products with categories.
How Does Eager Loading Work?
Eager Loading instructs Active Record to fetch associated records before they are accessed.
Depending on how the query is written, Rails may load associations using separate queries (includes or preload) or a SQL LEFT OUTER JOIN (eager_load). Rails automatically determines the most efficient strategy when using includes, unless the query requires a join.
Common methods include:
- includes – Loads associations upfront and lets Rails choose the most appropriate loading strategy.
- preload – Always loads associations using separate queries.
- eager_load – Always loads associations using a LEFT OUTER JOIN.
Examples
Scenario 1: The N+1 Query Problem
Without eager loading:
posts = Post.all posts.each do |post| puts post.author.name end
Rails executes:
- One query to load all posts
- One additional query for each post's author
If there are 100 posts, this results in 101 database queries.
Scenario 2: Using includes
posts = Post.includes(:author) posts.each do |post| puts post.author.name end
Rails loads all posts and their associated authors upfront, significantly reducing the number of database queries.
Scenario 3: Loading Multiple Associations
orders = Order.includes(:customer, :line_items) orders.each do |order| puts order.customer.name puts order.line_items.count end
Both the customer and line items are loaded in advance, avoiding additional queries while iterating through the orders.
Scenario 4: Nested Eager Loading
posts = Post.includes(comments: :user) posts.each do |post| post.comments.each do |comment| puts comment.user.name end end
This loads posts, their comments, and each comment's user efficiently, avoiding multiple levels of N+1 queries.
Where to Use Eager Loading?
- Index pages displaying lists of records with related data
- Dashboards showing information from multiple associated models
- APIs that return nested resources
- Reporting and analytics queries
- Any page where associated records are accessed repeatedly
In Summary
Eager Loading is a performance optimization technique in Rails that retrieves associated records before they are accessed. By using methods like includes, preload, or eager_load, developers can eliminate the N+1 query problem, reduce database queries, and build faster, more scalable applications.