Challenge - 5 Problems
Where Conditions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat is the output of this ActiveRecord query?
Given a Rails model
User with attributes age and active, what records does this query return?User.where(age: 30).where(active: true)Attempts:
2 left
💡 Hint
Remember that chaining
where clauses in Rails combines conditions with AND logic.✗ Incorrect
Chaining where calls in Rails adds conditions that must all be true. So User.where(age: 30).where(active: true) returns users who are both 30 years old and active.
📝 Syntax
intermediateWhich option correctly uses a hash condition with
where?Select the correct syntax to find all
Product records with category equal to 'books' and in_stock equal to true.Attempts:
2 left
💡 Hint
Use a Ruby hash with symbols for keys inside
where.✗ Incorrect
The correct syntax uses a hash with keys as symbols and values as the filter values. Option A is valid Ruby and Rails syntax.
🔧 Debug
advancedWhy does this
where query raise an error?Consider this code:
Why does it raise an error?
User.where('age > ?', 'twenty')Why does it raise an error?
Attempts:
2 left
💡 Hint
Check the data type expected for the age column.
✗ Incorrect
The age column expects a number. Passing the string 'twenty' causes a type mismatch error in the database query.
🧠 Conceptual
advancedWhat does this
where clause do?What is the effect of this query?
Order.where('created_at >= ?', 1.week.ago).where(status: ['shipped', 'delivered'])Attempts:
2 left
💡 Hint
The first
where filters by date, the second by status values.✗ Incorrect
The query filters orders created in the last week AND whose status is either 'shipped' or 'delivered'.
❓ state_output
expertWhat is the count of records returned by this query?
Given the following data in
- 3 products with
- 2 products with
- 1 product with
What is the result of:
Product table:- 3 products with
price 100- 2 products with
price 200- 1 product with
price 300What is the result of:
Product.where('price >= ?', 150).where.not(price: 300).countAttempts:
2 left
💡 Hint
Filter products with price >= 150, then exclude those with price 300.
✗ Incorrect
Products with price >= 150 are those with prices 200 (2 products) and 300 (1 product). Excluding price 300 leaves 2 products.
