Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'find' without an ID causes an error.
Using 'pluck' returns only specific columns, not full records.
✗ Incorrect
The all method fetches all records in one query.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The includes method eager loads associations to reduce queries.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' returns only users exactly 30 years old.
Using '<' returns users younger than 30.
✗ Incorrect
The operator > correctly filters users older than 30.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'false' fetches inactive users.
Using 'select' returns ActiveRecord objects, not just emails.
✗ Incorrect
Filtering active users with true and using pluck fetches only emails efficiently.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by :email changes the hash keys incorrectly.
Using '<' or '=' filters wrong post counts.
✗ Incorrect
Grouping by :name, filtering with > 5 posts, and counting returns the desired hash.