Challenge - 5 Problems
Select and Pluck Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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)Attempts:
2 left
💡 Hint
Remember that
pluck extracts specific column values as an array.✗ Incorrect
The where filters users by age 30, and pluck(:name) returns an array of their names only, not full objects or hashes.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
Think about the order of filtering and selecting columns.
✗ Incorrect
where filters first, then select limits columns, and pluck extracts the column values. Option B follows this order correctly.
🔧 Debug
advanced2:00remaining
Why does this code raise an error?
Given the code:
Why does it raise an error?
User.pluck(:name).where(age: 30)
Why does it raise an error?
Ruby on Rails
User.pluck(:name).where(age: 30)Attempts:
2 left
💡 Hint
Check what type
pluck returns and what methods are available on it.✗ Incorrect
pluck returns a plain Ruby array, which does not have ActiveRecord query methods like where. So calling where after pluck causes a NoMethodError.
❓ state_output
advanced2:00remaining
What is the output of this chained query?
Given the following code:
What does it return?
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)
Attempts:
2 left
💡 Hint
Remember what
pluck returns after selecting columns.✗ Incorrect
The where filters active users, select limits columns but pluck(:name) extracts only the names as an array of strings.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about what objects are created and how much data is loaded.
✗ Incorrect
pluck directly retrieves the specified columns as arrays from the database, skipping the creation of ActiveRecord objects, which makes it faster and uses less memory.