0
0
Ruby on Railsframework~10 mins

Query interface basics 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 from the database.

Ruby on Rails
users = User.[1]
Drag options to blanks, or click blank then click option'
Aselect
Bfind
Cwhere
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'find' without an ID causes an error.
Using 'where' requires conditions.
Using 'select' is for choosing columns, not records.
2fill in blank
medium

Complete the code to find users with the name 'Alice'.

Ruby on Rails
users = User.where(name: [1])
Drag options to blanks, or click blank then click option'
A'Alice'
B:Alice
CAlice
D"Alice"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol (:Alice) instead of a string.
Not quoting the string at all.
Using bare words without quotes.
3fill in blank
hard

Fix the error in the code to get the first user ordered by created_at.

Ruby on Rails
user = User.order(:created_at).[1]
Drag options to blanks, or click blank then click option'
Alast
Bfirst
Ctake
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last' returns the newest record, not the first.
Using 'find' requires an ID argument.
Using 'take' returns an array, while 'first' returns a single record.
4fill in blank
hard

Fill both blanks to get users created after 2023-01-01 and order by name.

Ruby on Rails
users = User.where("created_at > [1]").[2](:name)
Drag options to blanks, or click blank then click option'
A'2023-01-01'
Border
Csort
Dwhere
Attempts:
3 left
💡 Hint
Common Mistakes
Using sort instead of order.
Not quoting the date string.
Using where again instead of order.
5fill in blank
hard

Fill all three blanks to select users with age over 30, order by age descending, and limit to 5 results.

Ruby on Rails
users = User.where('age [1] [2]').order(age: [3]).limit(5)
Drag options to blanks, or click blank then click option'
A>
B30
C:desc
D:asc
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for age.
Ordering ascending instead of descending.
Quoting the number 30 in the where clause.