Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the where method do in Rails' query interface?
easy
A. Filters records based on given conditions
B. Deletes records from the database
C. Updates records with new values
D. Creates new records in the database

Solution

  1. Step 1: Understand the purpose of where

    The where method is used to filter records by specifying conditions, like filtering users by age.
  2. Step 2: Compare with other methods

    Methods like delete, update, and create perform different actions, not filtering.
  3. Final Answer:

    Filters records based on given conditions -> Option A
  4. Quick Check:

    where filters records [OK]
Hint: Remember: where = filter records by condition [OK]
Common Mistakes:
  • Confusing where with update or delete
  • Thinking where creates or deletes records
  • Assuming where changes data instead of filtering
2. Which of the following is the correct syntax to get all users ordered by their created_at date in descending order?
easy
A. User.order(:created_at, :desc)
B. User.order('created_at ASC')
C. User.order(created_at: :desc)
D. User.order(created_at => 'desc')

Solution

  1. Step 1: Identify correct hash syntax for order

    Rails expects a hash with column name as key and direction as symbol, like created_at: :desc.
  2. Step 2: Check other options for syntax errors

    User.order(:created_at, :desc) uses wrong argument format treating :desc as column name, User.order('created_at ASC') uses string with wrong direction, User.order(created_at => 'desc') uses wrong hash rocket syntax.
  3. Final Answer:

    User.order(created_at: :desc) -> Option C
  4. Quick Check:

    Correct hash syntax for order = User.order(created_at: :desc) [OK]
Hint: Use symbol keys and symbols for direction in order [OK]
Common Mistakes:
  • Passing multiple arguments instead of a hash
  • Using wrong direction like ASC in order string
  • Wrong hash syntax with => and string
3. Given the code Product.where(price: 100).limit(3).pluck(:name), what will be the output?
medium
A. An array of up to 3 product names with price 100
B. An array of all product names regardless of price
C. A single product name with price 100
D. An error because limit cannot be chained after where

Solution

  1. Step 1: Understand chaining of query methods

    where(price: 100) filters products with price 100, limit(3) restricts to 3 records, pluck(:name) extracts names into an array.
  2. Step 2: Analyze output type

    The result is an array of names, up to 3, matching the price condition.
  3. Final Answer:

    An array of up to 3 product names with price 100 -> Option A
  4. Quick Check:

    where + limit + pluck = filtered limited array [OK]
Hint: Chain filters, limits, then pluck for array output [OK]
Common Mistakes:
  • Thinking pluck returns a single value
  • Assuming limit cannot be chained after where
  • Ignoring the filter condition in output
4. What is wrong with this query? User.where('age > ?', 18).order('name ASC').limit('five')
medium
A. The where clause syntax is incorrect
B. The limit argument should be an integer, not a string
C. The order method cannot use strings
D. The query is correct and will run without errors

Solution

  1. Step 1: Check each method argument type

    where('age > ?', 18) is correct, order('name ASC') accepts string, but limit('five') expects an integer, not a string.
  2. Step 2: Identify the error source

    Passing 'five' as a string sets limit to 0 because 'five'.to_i == 0, not the intended value.
  3. Final Answer:

    The limit argument should be an integer, not a string -> Option B
  4. Quick Check:

    limit needs integer, not string [OK]
Hint: limit must be a number, not a word string [OK]
Common Mistakes:
  • Passing string instead of integer to limit
  • Thinking order cannot take strings
  • Misreading where clause syntax
5. You want to find all Article records published after 2020, sorted by title ascending, but only show the first 5 titles. Which query is correct?
hard
A. Article.where(published_at: > Date.new(2020,1,1)).order(:title).limit(5).pluck(:title)
B. Article.limit(5).order(title: :asc).where('published_at > 2020-01-01').pluck(:title)
C. Article.where('published_at > 2020-01-01').order('title DESC').limit(5).pluck(:title)
D. Article.where('published_at > ?', Date.new(2020,1,1)).order(title: :asc).limit(5).pluck(:title)

Solution

  1. Step 1: Check filtering condition syntax

    Article.where('published_at > ?', Date.new(2020,1,1)).order(title: :asc).limit(5).pluck(:title) uses correct SQL placeholder with where('published_at > ?', Date.new(2020,1,1)). Article.where(published_at: > Date.new(2020,1,1)).order(:title).limit(5).pluck(:title) has invalid Ruby syntax. Article.where('published_at > 2020-01-01').order('title DESC').limit(5).pluck(:title) uses string date without quotes and wrong order direction.
  2. Step 2: Verify order and limit usage

    Article.where('published_at > ?', Date.new(2020,1,1)).order(title: :asc).limit(5).pluck(:title) orders by title ascending with symbol and limits to 5 records before pluck. Article.limit(5).order(title: :asc).where('published_at > ?', '2020-01-01').pluck(:title) applies limit before where, which is less efficient and may give wrong results.
  3. Final Answer:

    Article.where('published_at > ?', Date.new(2020,1,1)).order(title: :asc).limit(5).pluck(:title) -> Option D
  4. Quick Check:

    Correct where, order ascending, limit 5, pluck titles [OK]
Hint: Filter first, then order, limit, and pluck [OK]
Common Mistakes:
  • Wrong where syntax with > operator
  • Ordering descending instead of ascending
  • Limiting before filtering causing wrong results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard