📖 Scenario: You are building a simple Rails app to manage a bookstore's inventory. You want to find books based on certain conditions like price and availability.
🎯 Goal: Learn how to use where conditions in Rails ActiveRecord queries to filter records from the database.
📋 What You'll Learn
Create a Book model with specific attributes
Add a variable to hold a price limit
Use where with a condition to find books cheaper than the price limit
Add a final query to find books that are both cheap and in stock
💡 Why This Matters
🌍 Real World
Filtering database records by conditions is common in web apps to show users only relevant data.
💼 Career
Knowing how to use ActiveRecord <code>where</code> queries is essential for Rails developers to build efficient and user-friendly applications.
Progress0 / 4 steps
1
Create the Book model with attributes
Create a Book model with these exact attributes: title:string, price:integer, and in_stock:boolean.
Ruby on Rails
Hint
In Rails, models inherit from ApplicationRecord. Attributes are defined in the database migration, but for this exercise, just define the model class.
2
Add a price limit variable
Add a variable called max_price and set it to 20 to represent the maximum price for filtering books.
Ruby on Rails
Hint
Just create a simple variable named max_price and assign the number 20.
3
Use where to find books cheaper than max_price
Write a query called cheap_books that uses Book.where to find books with price less than max_price.
Ruby on Rails
Hint
Use where("price < ?", max_price) to filter books cheaper than max_price.
4
Find books that are cheap and in stock
Write a query called available_cheap_books that uses Book.where with conditions to find books where price is less than max_price and in_stock is true.
Ruby on Rails
Hint
Use where("price < ? AND in_stock = ?", max_price, true) to filter books that are cheap and in stock.
Practice
(1/5)
1. What does the where method do in Rails ActiveRecord?
easy
A. Creates a new database table
B. Deletes records from the database
C. Updates all records without conditions
D. Filters records based on given conditions
Solution
Step 1: Understand the purpose of where
The where method is used to specify conditions to filter records in a database query.
Step 2: Compare with other database actions
Deleting, creating tables, or updating all records are different methods like delete, migrations, or update_all.
Final Answer:
Filters records based on given conditions -> Option D
Quick Check:
where filters records = C [OK]
Hint: Remember: where narrows down records by conditions [OK]
Common Mistakes:
Confusing where with delete or update methods
Thinking where creates or deletes tables
Assuming where modifies all records without conditions
2. Which of the following is the correct syntax to find users with age 30 using where?
easy
A. User.where('age == ?', 30)
B. User.where(age: 30)
C. User.where(age == 30)
D. User.where(age = 30)
Solution
Step 1: Identify correct hash syntax for where
Using a hash like age: 30 is the standard way to filter by attribute value.