0
0
Ruby on Railsframework~20 mins

Select and pluck in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Select and Pluck Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this ActiveRecord query return?
Consider a Rails model User with attributes id, name, and age. What is the output of this code?
User.where(age: 30).pluck(:name)
Ruby on Rails
User.where(age: 30).pluck(:name)
AA single string with all names concatenated for users aged 30
BAn array of User objects with age 30
CA hash with keys as user ids and values as names of users aged 30
DAn array of names of users who are 30 years old
Attempts:
2 left
💡 Hint
Remember that pluck extracts specific column values as an array.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses select and pluck together?
You want to get the names of users older than 25. Which code snippet is correct?
AUser.select(:name).where('age > ?', 25).pluck(:name)
BUser.where('age > ?', 25).select(:name).pluck(:name)
CUser.pluck(:name).select { |name| name.age > 25 }
DUser.where('age > ?', 25).pluck(:name).select(:name)
Attempts:
2 left
💡 Hint
Think about the order of filtering and selecting columns.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
Given the code:
User.pluck(:name).where(age: 30)

Why does it raise an error?
Ruby on Rails
User.pluck(:name).where(age: 30)
A<code>pluck</code> returns an array, which does not support <code>where</code>
B<code>where</code> must come before <code>pluck</code> because <code>pluck</code> returns a relation
CThe syntax is correct; the error is from missing parentheses
D<code>pluck</code> can only be used with <code>select</code> first
Attempts:
2 left
💡 Hint
Check what type pluck returns and what methods are available on it.
state_output
advanced
2:00remaining
What is the output of this chained query?
Given the following code:
User.where(active: true).select(:id, :name).pluck(:name)

What does it return?
Ruby on Rails
User.where(active: true).select(:id, :name).pluck(:name)
AAn array of names of active users
BAn array of hashes with id and name of active users
CAn array of User objects with only id and name loaded
DA single string concatenating all active user names
Attempts:
2 left
💡 Hint
Remember what pluck returns after selecting columns.
🧠 Conceptual
expert
3:00remaining
Why prefer pluck over select when fetching specific columns?
In Rails, when fetching only one or two columns from a large table, why might pluck be more efficient than select?
A<code>select</code> returns arrays while <code>pluck</code> returns ActiveRecord objects, making <code>pluck</code> faster
B<code>select</code> always loads all columns regardless of arguments, so it is slower
C<code>pluck</code> fetches only the requested columns directly from the database and returns plain Ruby arrays, avoiding object instantiation
D<code>pluck</code> caches the results automatically, improving performance
Attempts:
2 left
💡 Hint
Think about what objects are created and how much data is loaded.