Challenge - 5 Problems
Eager Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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
Attempts:
2 left
💡 Hint
Think about how Rails loads associations by default.
✗ Incorrect
Without eager loading, Rails loads posts in one query, then loads comments count for each post separately, causing N additional queries.
📝 Syntax
intermediate2: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.
Attempts:
2 left
💡 Hint
Look for the method that loads associations in one query or minimal queries.
✗ Incorrect
includes loads associated comments efficiently to avoid N+1 queries. joins only joins tables but does not eager load. preload loads separately but does not filter well. eager_load forces LEFT OUTER JOIN and can cause complex queries.
🔧 Debug
advanced2:00remaining
Why does this eager loading code still cause N+1 queries?
Consider this code snippet:
Why does it still cause multiple queries?
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
Attempts:
2 left
💡 Hint
Think about how where on an association affects eager loading.
✗ Incorrect
Using where on the association inside the loop triggers a new query per post, causing N+1 despite includes.
🧠 Conceptual
advanced2: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.
Attempts:
2 left
💡 Hint
Think about how each method loads associated records.
✗ Incorrect
includes can use JOIN or separate queries depending on usage, preload always uses separate queries.
❓ state_output
expert2: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
endRuby 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
endAttempts:
2 left
💡 Hint
eager_load uses JOIN and loads all data in one query.
✗ Incorrect
eager_load uses LEFT OUTER JOIN and loads posts with filtered comments in one query. Accessing comments.size does not trigger extra queries.