0
0
Ruby on Railsframework~10 mins

Database query optimization in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to fetch all users with a single query.

Ruby on Rails
users = User.[1]
Drag options to blanks, or click blank then click option'
Afind
Bwhere
Call
Dpluck
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'find' without an ID causes an error.
Using 'pluck' returns only specific columns, not full records.
2fill in blank
medium

Complete the code to eager load associated posts with users.

Ruby on Rails
users = User.[1](:posts)
Drag options to blanks, or click blank then click option'
Agroup
Bjoins
Cselect
Dincludes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'joins' does not eager load and can cause N+1 queries.
Using 'select' changes columns returned but does not eager load.
3fill in blank
hard

Fix the error in the query to find users older than 30.

Ruby on Rails
users = User.where("age [1] ?", 30)
Drag options to blanks, or click blank then click option'
A>
B!=
C<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' returns only users exactly 30 years old.
Using '<' returns users younger than 30.
4fill in blank
hard

Fill both blanks to optimize fetching user emails for active users.

Ruby on Rails
emails = User.where(active: [1]).[2](:email)
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Cpluck
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' fetches inactive users.
Using 'select' returns ActiveRecord objects, not just emails.
5fill in blank
hard

Fill all three blanks to create a hash of user names and their post counts for users with more than 5 posts.

Ruby on Rails
result = User.joins(:posts).group([1]).having("COUNT(posts.id) [2] ?", [3]).count
Drag options to blanks, or click blank then click option'
A:name
B>
C5
D:email
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by :email changes the hash keys incorrectly.
Using '<' or '=' filters wrong post counts.