0
0
Ruby on Railsframework~30 mins

Query interface basics in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Query Interface Basics in Rails
📖 Scenario: You are building a simple Rails app to manage a bookstore's inventory. You want to find books based on their price and availability.
🎯 Goal: Learn how to use Rails query interface methods to filter and retrieve records from the database.
📋 What You'll Learn
Create a Book model with specific attributes
Set a price threshold variable
Use ActiveRecord query methods to find books cheaper than the threshold
Add a condition to find only books that are in stock
💡 Why This Matters
🌍 Real World
Filtering and searching records is a common task in web apps like online stores or libraries.
💼 Career
Understanding Rails query interface is essential for backend developers working with databases.
Progress0 / 4 steps
1
Create the Book model with attributes
Create a Rails model called Book with attributes title (string), price (integer), and in_stock (boolean). Then create three books with these exact values: 'Ruby Basics' priced at 30 and true for in_stock, 'Rails Advanced' priced at 50 and false for in_stock, and 'JavaScript Guide' priced at 20 and true for in_stock.
Ruby on Rails
Need a hint?

Use Book.create to add records with the exact attribute values.

2
Set a price threshold variable
Create a variable called max_price and set it to 40. This will be used to find books cheaper than this price.
Ruby on Rails
Need a hint?

Just assign the number 40 to a variable named max_price.

3
Find books cheaper than max_price
Use the Rails query interface to find all books with price less than max_price. Assign the result to a variable called cheap_books. Use where with a condition on price.
Ruby on Rails
Need a hint?

Use Book.where('price < ?', max_price) to filter books cheaper than max_price.

4
Find books cheaper than max_price and in stock
Update the query to find books with price less than max_price and in_stock equal to true. Assign the result to a variable called available_cheap_books. Use where with multiple conditions.
Ruby on Rails
Need a hint?

Use where with SQL conditions joined by AND to filter by price and stock.