Recall & Review
beginner
What does the
select method do in Rails ActiveRecord?The
select method lets you choose specific columns from the database to retrieve, instead of loading all columns. It returns ActiveRecord objects with only those columns filled.Click to reveal answer
beginner
How is
pluck different from select in Rails?pluck directly fetches the values of specified columns as an array, without creating ActiveRecord objects. It's faster when you only need raw data.Click to reveal answer
beginner
What type of result does
pluck(:name) return?It returns an array of values from the
name column, like ["Alice", "Bob", "Carol"].Click to reveal answer
intermediate
Can you chain
select with other ActiveRecord query methods?Yes,
select can be chained with methods like where, order, and limit to build complex queries.Click to reveal answer
intermediate
Why might you choose
pluck over select when fetching data?Because
pluck is faster and uses less memory by returning plain arrays instead of full ActiveRecord objects, making it ideal for simple data extraction.Click to reveal answer
What does
User.select(:email) return?✗ Incorrect
select returns ActiveRecord objects but only loads the specified columns.Which method returns an array of values directly from the database?
✗ Incorrect
pluck fetches column values as an array without creating objects.If you want to get just the names of users as an array, which method do you use?
✗ Incorrect
pluck(:name) returns an array of names directly.Can
pluck be chained with where?✗ Incorrect
You can chain
where before pluck to filter results.Which method is better for performance when you only need one column's data?
✗ Incorrect
pluck is faster and uses less memory for single column data.Explain in your own words the difference between
select and pluck in Rails ActiveRecord.Think about what you get back and how much memory is used.
You got /4 concepts.
Describe a situation where you would prefer to use
pluck instead of select.Imagine you just want a list of emails for a newsletter.
You got /4 concepts.