Eager loading helps your app get related data all at once. This stops slowdowns caused by asking the database many times.
Eager loading (N+1 prevention) in Ruby on Rails
Model.includes(:association)
Use includes with the name of the related data you want to load.
This tells Rails to load the main data and its related data in fewer queries.
Post.includes(:author)
Article.includes(:comments)
Product.includes(:category, :supplier)
This controller loads all posts and their authors together. The view then shows each post's title and author name without extra database queries.
class PostsController < ApplicationController def index @posts = Post.includes(:author).all end end # In the view (index.html.erb): # <% @posts.each do |post| %> # <p><strong><%= post.title %></strong> by <%= post.author.name %></p> # <% end %>
Without eager loading, accessing post.author for each post causes a new database query.
Eager loading reduces database calls, making your app faster and more efficient.
Use Rails console and logs to see how many queries run with and without includes.
Eager loading loads related data in fewer queries.
Use includes to prevent the N+1 query problem.
This makes your app faster and smoother for users.
Practice
includes in Rails when querying data?Solution
Step 1: Understand N+1 query problem
When loading records with associations, Rails may run one query for the main records and one query per associated record, causing many queries.Step 2: Role of
Theincludesincludesmethod tells Rails to load all associated records in one or few queries, preventing many extra queries.Final Answer:
To load associated records in fewer database queries and avoid N+1 problems -> Option AQuick Check:
Eager loading = fewer queries = better speed [OK]
- Thinking includes deletes records
- Confusing includes with sorting
- Assuming includes creates new records
comments association for Post records?Solution
Step 1: Identify the standard eager loading method
Rails usesincludesto eager load associations simply and efficiently.Step 2: Check syntax correctness
Post.includes(:comments)is the correct syntax to load posts with their comments in fewer queries.Final Answer:
Post.includes(:comments) -> Option CQuick Check:
Correct eager loading syntax = includes [OK]
- Using non-existent methods like load_comments
- Confusing eager_load with includes (different behavior)
- Using with_comments which is not a Rails method
posts = Post.all posts.each do |post| puts post.comments.count end
Solution
Step 1: Analyze the code without eager loading
Post.allloads all posts in 1 query. Then inside the loop,post.comments.counttriggers a query per post to count comments.Step 2: Calculate total queries
1 query for posts + N queries for comments count = N+1 queries total.Final Answer:
N+1 queries, where N is the number of posts -> Option BQuick Check:
Without includes, each association call triggers a query [OK]
- Assuming only 1 query runs
- Thinking count does not query database
- Confusing eager_load with includes
posts = Post.includes(:comments).where(published: true) posts.each do |post| puts post.comments.count end
What is the likely cause of the N+1 queries still happening?
Solution
Step 1: Understand how
Callingcountworks on associationspost.comments.countruns a SQL COUNT query for each post, ignoring preloaded records.Step 2: Why eager loading doesn't prevent count queries
includesloads all comments, butcountasks database for count separately, causing N+1 queries.Final Answer:
Usingcounton association triggers a separate query despite eager loading -> Option DQuick Check:
count triggers query even with includes [OK]
- Thinking where disables includes
- Believing joins always eager load
- Assuming eager loading needs all method
Solution
Step 1: Understand nested associations eager loading
To load authors with books and each book's reviews, nested includes syntax is needed.Step 2: Choose correct nested includes syntax
Author.includes(books: :reviews)loads authors, their books, and reviews in minimal queries.Step 3: Why other options are incorrect
Author.includes(:books, :reviews) tries to load reviews directly on authors (wrong association). Author.joins(:books).joins(:reviews) uses joins which may not eager load. Author.eager_load(:books, :reviews) eager_load loads with joins but is less flexible.Final Answer:
Author.includes(books: :reviews) -> Option AQuick Check:
Nested includes = includes(parent: :child) [OK]
- Trying to include unrelated associations directly
- Using joins instead of includes for eager loading
- Misunderstanding nested includes syntax
Ready to prove it?
Pass the test and this pattern moves unknown
