📖 Scenario: You are building a simple blog application where each Post has many Comments. You want to display a list of posts along with their comments efficiently.
🎯 Goal: Learn how to use Rails includes method to eager load associated comments for posts, preventing the N+1 query problem.
📋 What You'll Learn
Create a Post model with a has_many :comments association
Create a Comment model with a belongs_to :post association
Write a query to fetch all posts
Add eager loading of comments using includes to prevent N+1 queries
Display posts with their comments in a view
💡 Why This Matters
🌍 Real World
Eager loading is used in web applications to efficiently load related data and avoid slow database queries that happen when loading associations one by one.
💼 Career
Understanding and preventing N+1 queries is a key skill for Rails developers to build fast and scalable applications.
Progress0 / 4 steps
1
Set up Post and Comment models with associations
Create a Post model with has_many :comments and a Comment model with belongs_to :post associations.
Ruby on Rails
Hint
Use has_many in Post and belongs_to in Comment to set up associations.
2
Fetch all posts without eager loading
Write a query to fetch all posts and assign it to a variable called posts using Post.all.
Ruby on Rails
Hint
Use Post.all to get all posts and assign to posts.
3
Add eager loading of comments to prevent N+1 queries
Modify the query to fetch all posts with their comments eagerly loaded using includes(:comments) and assign it to posts.
Ruby on Rails
Hint
Use Post.includes(:comments).all to eager load comments with posts.
4
Display posts with their comments in a view
In a Rails view, use a each loop to iterate over posts and display each post's title and its comments' content.
Ruby on Rails
Hint
Use nested each loops to show posts and their comments.
Practice
(1/5)
1. What is the main purpose of using includes in Rails when querying data?
easy
A. To load associated records in fewer database queries and avoid N+1 problems
B. To delete associated records automatically
C. To sort records alphabetically
D. To create new associated records
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 includes
The includes method 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 A
Quick Check:
Eager loading = fewer queries = better speed [OK]
Hint: Remember: includes = fewer queries, faster loading [OK]
Common Mistakes:
Thinking includes deletes records
Confusing includes with sorting
Assuming includes creates new records
2. Which of the following is the correct syntax to eager load the comments association for Post records?
easy
A. Post.eager_load(:comments)
B. Post.load_comments
C. Post.includes(:comments)
D. Post.with_comments
Solution
Step 1: Identify the standard eager loading method
Rails uses includes to 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 C
Quick Check:
Correct eager loading syntax = includes [OK]
Hint: Use includes(:association) to eager load in Rails [OK]
Common Mistakes:
Using non-existent methods like load_comments
Confusing eager_load with includes (different behavior)
Using with_comments which is not a Rails method
3. Given the code below, what will be the total number of database queries executed?
posts = Post.all
posts.each do |post|
puts post.comments.count
end
medium
A. 1 query
B. N+1 queries, where N is the number of posts
C. 2 queries
D. No queries
Solution
Step 1: Analyze the code without eager loading
Post.all loads all posts in 1 query. Then inside the loop, post.comments.count triggers 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 B
Quick Check:
Without includes, each association call triggers a query [OK]
Hint: Looping over associations without includes causes N+1 queries [OK]
Common Mistakes:
Assuming only 1 query runs
Thinking count does not query database
Confusing eager_load with includes
4. You wrote this code to eager load comments but still see many queries:
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?
medium
A. Eager loading only works with all method
B. The where clause disables eager loading
C. You must use joins instead of includes
D. Using count on association triggers a separate query despite eager loading
Solution
Step 1: Understand how count works on associations
Calling post.comments.count runs a SQL COUNT query for each post, ignoring preloaded records.
includes loads all comments, but count asks database for count separately, causing N+1 queries.
Final Answer:
Using count on association triggers a separate query despite eager loading -> Option D
Quick Check:
count triggers query even with includes [OK]
Hint: Use size or length on loaded associations to avoid extra queries [OK]
Common Mistakes:
Thinking where disables includes
Believing joins always eager load
Assuming eager loading needs all method
5. You want to display a list of authors with their books and each book's reviews efficiently. Which is the best way to eager load these nested associations in Rails?
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 A
Quick Check:
Nested includes = includes(parent: :child) [OK]
Hint: Use nested hash in includes for nested associations [OK]
Common Mistakes:
Trying to include unrelated associations directly
Using joins instead of includes for eager loading
Misunderstanding nested includes syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.