Recall & Review
beginner Click to reveal answer
What does the
where method do in Rails ActiveRecord?The
where method filters database records based on given conditions and returns matching records as an ActiveRecord relation.beginner Click to reveal answer
How do you write a
where condition to find users with age 30?You write:
User.where(age: 30). This finds all users whose age is exactly 30.intermediate Click to reveal answer
Can you use multiple conditions in a single
where call? How?Yes. Pass a hash with multiple keys:
User.where(age: 30, active: true) finds users aged 30 who are active.intermediate Click to reveal answer
How do you use SQL fragments in
where for more complex queries?You can pass a string with placeholders:
User.where("age > ?", 25) finds users older than 25.intermediate Click to reveal answer
What is the benefit of chaining multiple
where calls?Chaining
where calls combines conditions with AND, making queries easier to read and build step-by-step.What does
User.where(active: true) return?✗ Incorrect
The
where method filters users with active set to true.How do you find users with age greater than 20 using
where?✗ Incorrect
Use a SQL fragment with placeholder:
User.where('age > ?', 20).What happens if you chain
where calls like User.where(active: true).where(age: 30)?✗ Incorrect
Chained
where calls combine conditions with AND.Which of these is a valid way to use
where with multiple conditions?✗ Incorrect
All shown ways are valid to specify multiple conditions.
What type of object does
where return?✗ Incorrect
where returns an ActiveRecord Relation, which can be further chained.Explain how to use the
where method in Rails to filter records with multiple conditions.Think about combining conditions with AND and using hashes or SQL strings.
You got /3 concepts.
Describe the difference between passing a hash and a SQL string to the
where method.Consider when you need more than just equals comparisons.
You got /3 concepts.
