0
0
Ruby on Railsframework~5 mins

Select and pluck in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AActiveRecord objects with only the email field loaded
BAn array of email strings
CAll user records with all fields
DA count of users with emails
Which method returns an array of values directly from the database?
Aselect
Bpluck
Cwhere
Dorder
If you want to get just the names of users as an array, which method do you use?
AUser.pluck(:name)
BUser.order(:name)
CUser.where(name: true)
DUser.select(:name)
Can pluck be chained with where?
AOnly with order, not where
BNo, pluck cannot be chained
COnly with select, not where
DYes, pluck can be chained after where
Which method is better for performance when you only need one column's data?
Aall
Bselect
Cpluck
Dfind
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.