0
0
Ruby on Railsframework~20 mins

N+1 detection tools in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
N+1 Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Detecting N+1 queries with Bullet gem

Given a Rails app using the Bullet gem, what will Bullet report when rendering a view that loads Post records and then accesses each post's comments without eager loading?

Ruby on Rails
posts = Post.all
posts.each do |post|
  post.comments.each do |comment|
    puts comment.body
  end
end
ABullet will report a syntax error due to missing eager loading.
BBullet will not report any warnings because Post.all loads all posts at once.
CBullet will report an N+1 query warning because comments are accessed without eager loading.
DBullet will report a performance warning unrelated to N+1 queries.
Attempts:
2 left
💡 Hint

Think about what happens when you access an association inside a loop without eager loading.

📝 Syntax
intermediate
2:00remaining
Correct usage of includes to avoid N+1

Which option correctly uses includes to avoid N+1 queries when loading posts and their comments?

Ruby on Rails
posts = Post.???
posts.each do |post|
  post.comments.each do |comment|
    puts comment.body
  end
end
Aselect(:comments)
Bincludes(:comments)
Cpreload(:comments).where('comments.count > 0')
Djoins(:comments)
Attempts:
2 left
💡 Hint

Which method loads associated records to prevent extra queries?

🔧 Debug
advanced
2:00remaining
Identifying cause of false positive N+1 detection

Using the Bullet gem, you get an N+1 warning on user.profile even though you used includes(:profile). What is the most likely cause?

Ruby on Rails
users = User.includes(:profile).where(active: true)
users.each do |user|
  puts user.profile.bio
end
AThe <code>profile</code> association uses a custom SQL that disables eager loading.
BYou forgot to call <code>references(:profile)</code> along with <code>includes</code>.
CThe <code>profile</code> association is defined with <code>optional: true</code>, causing Bullet to misdetect.
DBullet cannot detect N+1 queries on <code>has_one</code> associations.
Attempts:
2 left
💡 Hint

Think about how custom SQL in associations affects eager loading.

🧠 Conceptual
advanced
2:00remaining
Understanding Skylight's N+1 detection approach

How does Skylight detect N+1 queries in a Rails application?

ABy scanning the source code for missing <code>includes</code> calls in ActiveRecord queries.
BBy monitoring CPU usage spikes during database calls.
CBy requiring manual annotations in the code to mark potential N+1 spots.
DBy analyzing the number of similar SQL queries executed during a single request and flagging repeated queries on associations.
Attempts:
2 left
💡 Hint

Think about how a tool can detect repeated queries dynamically.

state_output
expert
3:00remaining
Output of Rack Mini Profiler on N+1 queries

Given this Rails controller code, what output will Rack Mini Profiler show regarding database queries?

def index
  @users = User.all
  @users.each do |user|
    user.posts.each do |post|
      puts post.title
    end
  end
end
AIt will show 1 query for users and multiple queries for posts, indicating an N+1 problem.
BIt will show a single optimized query joining users and posts.
CIt will show no database queries because data is cached.
DIt will show a syntax error in the controller code.
Attempts:
2 left
💡 Hint

Consider how accessing associations inside loops affects query count.