0
0
Ruby on Railsframework~20 mins

Query interface basics in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this ActiveRecord query?
Given a Rails model User with records having age attributes, what does this query return?
User.where(age: 20..30).pluck(:name)
AAn array of user ages between 20 and 30
BAn array of all user names regardless of age
CAn array of names of users whose age is between 20 and 30 inclusive
DA hash with user names as keys and ages as values for users aged 20 to 30
Attempts:
2 left
💡 Hint
Think about what where and pluck do in ActiveRecord.
📝 Syntax
intermediate
2:00remaining
Which option correctly finds users created after 2020-01-01?
Select the correct ActiveRecord query to find users created after January 1, 2020.
AUser.where('created_at > ?', '2020-01-01')
BUser.where(created_at: '> 2020-01-01')
CUser.where(created_at > '2020-01-01')
DUser.where(created_at: ['2020-01-01'..])
Attempts:
2 left
💡 Hint
Remember how to use raw SQL conditions with placeholders in where.
state_output
advanced
2:00remaining
What is the count of users returned by this query?
Assuming the users table has 10 records with active true and 5 with false, what does this return?
User.where(active: true).limit(3).count
A5
B10
C15
D3
Attempts:
2 left
💡 Hint
Remember how count works with limit in ActiveRecord.
🔧 Debug
advanced
2:00remaining
Why does this query raise an error?
Given the query:
User.where(name: 'Alice').order(:nonexistent_column)

Why does it raise an error?
ABecause <code>nonexistent_column</code> does not exist in the users table
BBecause <code>name</code> is not a valid attribute
CBecause <code>order</code> cannot be used after <code>where</code>
DBecause <code>where</code> requires a block
Attempts:
2 left
💡 Hint
Check the column names in the database schema.
🧠 Conceptual
expert
2:00remaining
Which option best describes lazy loading in ActiveRecord queries?
Select the statement that correctly explains lazy loading behavior in ActiveRecord query interface.
AQueries are executed in the background asynchronously by default
BQueries are executed only when the data is actually needed, like when iterating or accessing records
CQueries are never executed automatically and must be manually triggered
DQueries are executed immediately when the query method is called
Attempts:
2 left
💡 Hint
Think about when ActiveRecord fetches data from the database.