Bird
Raised Fist0
Ruby on Railsframework~20 mins

Eager loading (N+1 prevention) in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Eager Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output count of queries executed?
Given the following Rails code, how many SQL queries will be executed when rendering the posts and their comments count?

posts = Post.all
posts.each do |post|
  puts post.comments.count
end
Ruby on Rails
posts = Post.all
posts.each do |post|
  puts post.comments.count
end
A1 + N queries (N = number of posts)
B1 query
CN queries
D2 queries
Attempts:
2 left
💡 Hint
Think about how Rails loads associations by default.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly eager loads comments for posts?
Select the Rails code that correctly eager loads comments to prevent N+1 queries when accessing post.comments.
Aposts = Post.preload(:comments).where(comments: { approved: true })
Bposts = Post.joins(:comments).all
Cposts = Post.includes(:comments).all
Dposts = Post.eager_load(:comments).where('comments.created_at > ?', 1.week.ago)
Attempts:
2 left
💡 Hint
Look for the method that loads associations in one query or minimal queries.
🔧 Debug
advanced
2:00remaining
Why does this eager loading code still cause N+1 queries?
Consider this code snippet:

posts = Post.includes(:comments)
posts.each do |post|
  puts post.comments.where(approved: true).count
end

Why does it still cause multiple queries?
Ruby on Rails
posts = Post.includes(:comments)
posts.each do |post|
  puts post.comments.where(approved: true).count
end
ABecause filtering with where on association triggers a new query per post
BBecause includes does not load comments at all
CBecause count always triggers a single query for all posts
DBecause posts is not loaded before iteration
Attempts:
2 left
💡 Hint
Think about how where on an association affects eager loading.
🧠 Conceptual
advanced
2:00remaining
What is the main difference between includes and preload in Rails?
Choose the statement that best describes the difference between includes and preload methods for eager loading associations.
Aincludes only works with belongs_to, preload only with has_many
Bincludes always loads associations in separate queries, preload uses JOIN
Cincludes and preload are identical in behavior and performance
Dincludes uses LEFT OUTER JOIN, preload loads associations in separate queries
Attempts:
2 left
💡 Hint
Think about how each method loads associated records.
❓ state_output
expert
2:00remaining
What is the output of this Rails console code regarding query count?
Given the following code in Rails console, what will be the total number of SQL queries executed?

ActiveRecord::Base.logger = Logger.new(STDOUT)
posts = Post.eager_load(:comments).where(comments: { approved: true }).distinct
posts.each do |post|
  puts post.comments.size
end
Ruby on Rails
ActiveRecord::Base.logger = Logger.new(STDOUT)
posts = Post.eager_load(:comments).where(comments: { approved: true }).distinct
posts.each do |post|
  puts post.comments.size
end
A2 queries
B1 query
CN + 1 queries
DNo queries
Attempts:
2 left
💡 Hint
eager_load uses JOIN and loads all data in one query.

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

  1. 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.
  2. Step 2: Role of includes

    The includes method tells Rails to load all associated records in one or few queries, preventing many extra queries.
  3. Final Answer:

    To load associated records in fewer database queries and avoid N+1 problems -> Option A
  4. 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

  1. Step 1: Identify the standard eager loading method

    Rails uses includes to eager load associations simply and efficiently.
  2. Step 2: Check syntax correctness

    Post.includes(:comments) is the correct syntax to load posts with their comments in fewer queries.
  3. Final Answer:

    Post.includes(:comments) -> Option C
  4. 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

  1. 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.
  2. Step 2: Calculate total queries

    1 query for posts + N queries for comments count = N+1 queries total.
  3. Final Answer:

    N+1 queries, where N is the number of posts -> Option B
  4. 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

  1. Step 1: Understand how count works on associations

    Calling post.comments.count runs a SQL COUNT query for each post, ignoring preloaded records.
  2. Step 2: Why eager loading doesn't prevent count queries

    includes loads all comments, but count asks database for count separately, causing N+1 queries.
  3. Final Answer:

    Using count on association triggers a separate query despite eager loading -> Option D
  4. 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?
hard
A. Author.includes(books: :reviews)
B. Author.includes(:books, :reviews)
C. Author.joins(:books).joins(:reviews)
D. Author.eager_load(:books, :reviews)

Solution

  1. Step 1: Understand nested associations eager loading

    To load authors with books and each book's reviews, nested includes syntax is needed.
  2. Step 2: Choose correct nested includes syntax

    Author.includes(books: :reviews) loads authors, their books, and reviews in minimal queries.
  3. 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.
  4. Final Answer:

    Author.includes(books: :reviews) -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard