0
0
Ruby on Railsframework~10 mins

Select and pluck in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Select and pluck
Start with ActiveRecord Relation
Call select(columns)
Database query returns objects with selected columns
Call pluck(column)
Database query returns array of values for that column
End
You start with a database query, use select to choose columns, then pluck to get an array of values from a column.
Execution Sample
Ruby on Rails
users = User.select(:id, :name)
names = users.pluck(:name)
Select returns user objects with only id and name; pluck returns an array of all user names.
Execution Table
StepActionQuery SentResult TypeResult Example
1Call User.select(:id, :name)SELECT id, name FROM usersActiveRecord::Relation (objects)[#<User id:1, name:"Alice">, #<User id:2, name:"Bob">]
2Call users.pluck(:name)SELECT name FROM usersArray of values["Alice", "Bob"]
3Use names arrayNo queryArray["Alice", "Bob"]
💡 No more queries; pluck returns array, select returns objects with selected columns.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
usersnil[User objects with id and name][User objects unchanged][User objects with id and name]
namesnilnil["Alice", "Bob"]["Alice", "Bob"]
Key Moments - 2 Insights
Why does select return objects but pluck returns an array?
Select builds objects with only chosen columns (see execution_table step 1), while pluck directly fetches column values as an array (step 2).
Can I use pluck after select to get an array?
Yes, pluck can be called on the relation returned by select, and it will send its own query to fetch the specified column values as an array.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what type of result does User.select(:id, :name) return at step 1?
AArray of values
BSingle value
CActiveRecord objects with selected columns
DNil
💡 Hint
Check the 'Result Type' column in execution_table row for step 1.
At which step does the database return an array of names?
AStep 2
BStep 1
CStep 3
DNo step returns an array
💡 Hint
Look at the 'Result Type' and 'Result Example' columns in execution_table.
If you want an array of user ids, which method should you use?
AUser.select(:id).to_a
BUser.pluck(:id)
CUser.select(:id).pluck(:id)
DUser.all
💡 Hint
Refer to how pluck returns arrays directly in execution_table step 2.
Concept Snapshot
select(:columns) returns ActiveRecord objects with only those columns.
pluck(:column) returns an array of values for that column.
select builds objects; pluck fetches raw values.
pluck sends its own query, ignoring select.
Use pluck for simple arrays, select for partial objects.
Full Transcript
In Rails, select and pluck are ways to get data from the database. Select chooses which columns to include in the returned objects. For example, User.select(:id, :name) returns user objects with only id and name fields. Pluck gets an array of values for a single column, like User.pluck(:name) returns an array of all user names. Select returns ActiveRecord objects, pluck returns simple arrays. Pluck sends its own query and does not use select's columns. This means pluck is faster when you only want one column's values. Understanding this helps you write efficient queries and get the data format you want.