0
0
Ruby on Railsframework~30 mins

Select and pluck in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Select and Pluck in Rails
📖 Scenario: You are building a simple Rails app to manage a bookstore. You have a list of books stored in the database with details like title, author, and price.You want to practice extracting specific information from the books using Rails ActiveRecord methods.
🎯 Goal: Learn how to use select to filter records and pluck to extract specific fields from the database in Rails.
📋 What You'll Learn
Create a Book model with attributes: title (string), author (string), price (integer)
Use select to filter books priced above a certain amount
Use pluck to get a list of book titles from the filtered books
💡 Why This Matters
🌍 Real World
Filtering and extracting specific data from a database is common in web apps like bookstores, inventory systems, or user management.
💼 Career
Understanding select and pluck helps you write efficient Rails queries to get exactly the data you need without extra processing.
Progress0 / 4 steps
1
Create the books data
Create a variable called books and assign it an array of hashes representing books with these exact entries: { title: 'Ruby Basics', author: 'Alice', price: 30 }, { title: 'Rails Advanced', author: 'Bob', price: 50 }, { title: 'JavaScript Guide', author: 'Carol', price: 40 }.
Ruby on Rails
Need a hint?

Use an array of hashes with the exact keys and values given.

2
Set the price threshold
Create a variable called price_threshold and set it to 35.
Ruby on Rails
Need a hint?

Just assign the number 35 to the variable price_threshold.

3
Filter books with select
Create a variable called expensive_books and assign it the result of using select on books to keep only books where the price is greater than price_threshold.
Ruby on Rails
Need a hint?

Use select with a block that checks if book[:price] is greater than price_threshold.

4
Extract titles with pluck
Create a variable called titles and assign it the result of using pluck on expensive_books to get an array of their title values.
Ruby on Rails
Need a hint?

Use map with a block to extract the :title from each book hash.