Bird
Raised Fist0
Ruby on Railsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between select and pluck in Rails ActiveRecord?
easy
A. select returns all columns, pluck returns only the first column.
B. select returns model objects with selected columns, pluck returns an array of values.
C. select modifies the database, pluck deletes records.
D. select returns an array of values, pluck returns model objects.

Solution

  1. Step 1: Understand select behavior

    select loads model objects but only with the specified columns, so you still get ActiveRecord objects.
  2. Step 2: Understand pluck behavior

    pluck fetches column values directly from the database and returns them as a simple array, not objects.
  3. Final Answer:

    select returns model objects with selected columns, pluck returns an array of values. -> Option B
  4. Quick Check:

    Select = objects, Pluck = array [OK]
Hint: Select returns objects, pluck returns arrays of values [OK]
Common Mistakes:
  • Thinking pluck returns objects
  • Confusing select with all columns
  • Assuming select modifies data
2. Which of the following is the correct syntax to get an array of user emails using pluck?
easy
A. User.pluck(:email)
B. User.select(:email).pluck
C. User.pluck.email
D. User.select.pluck(:email)

Solution

  1. Step 1: Identify correct method usage

    pluck is called on the model with the column symbol as argument, like User.pluck(:email).
  2. Step 2: Check other options for syntax errors

    Options A, B, and C misuse method chaining or syntax and will cause errors.
  3. Final Answer:

    User.pluck(:email) -> Option A
  4. Quick Check:

    Correct pluck syntax = User.pluck(:column) [OK]
Hint: Use Model.pluck(:column) to get array of values [OK]
Common Mistakes:
  • Calling pluck without parentheses
  • Trying to chain pluck after select incorrectly
  • Using dot notation like pluck.email
3. Given the following code, what will be the output?
users = User.select(:id, :name).where(active: true)
names = users.pluck(:name)
medium
A. An error because pluck cannot be called after select
B. An array of all user names regardless of active status
C. An array of names of active users
D. An array of User objects with id and name

Solution

  1. Step 1: Analyze the query chain

    User.select(:id, :name).where(active: true) returns ActiveRecord objects with only id and name for active users.
  2. Step 2: Understand pluck on the relation

    Calling pluck(:name) on this relation fetches the name column values directly from the database for active users.
  3. Final Answer:

    An array of names of active users -> Option C
  4. Quick Check:

    Pluck after where filters data correctly [OK]
Hint: Pluck after where returns filtered column values [OK]
Common Mistakes:
  • Thinking pluck returns objects
  • Assuming pluck ignores where filters
  • Believing pluck cannot follow select
4. Identify the error in this code snippet:
users = User.pluck(:id, :name).select { |u| u.active? }
medium
A. No error, code works fine
B. Pluck cannot take multiple columns
C. Select cannot be called after pluck
D. Calling active? on array elements causes error

Solution

  1. Step 1: Understand what pluck returns

    pluck(:id, :name) returns an array of arrays like [[1, "Alice"], [2, "Bob"]], not User objects.
  2. Step 2: Check usage of active?

    Since elements are arrays, calling active? method on them causes a NoMethodError.
  3. Final Answer:

    Calling active? on array elements causes error -> Option D
  4. Quick Check:

    Pluck returns arrays, not objects [OK]
Hint: Pluck returns arrays, not objects with methods [OK]
Common Mistakes:
  • Assuming pluck returns objects
  • Trying to call model methods on pluck results
  • Thinking select after pluck filters objects
5. You want to get a list of unique active user emails efficiently. Which code snippet is best?
hard
A. User.where(active: true).pluck(:email).uniq
B. User.where(active: true).select(:email).map(&:email).uniq
C. User.select(:email).where(active: true).pluck(:email).uniq
D. User.pluck(:email).where(active: true).uniq

Solution

  1. Step 1: Analyze efficiency of each option

    User.where(active: true).select(:email).map(&:email).uniq loads objects with select then maps emails, which loads more data than needed.
  2. Step 2: Check correct chaining and method usage

    User.where(active: true).pluck(:email).uniq uses where to filter active users, then pluck to get emails directly from DB, then uniq to remove duplicates efficiently.
  3. Step 3: Identify errors in other options

    User.select(:email).where(active: true).pluck(:email).uniq is redundant but works; User.pluck(:email).where(active: true).uniq is invalid because pluck returns array, so where after pluck causes error.
  4. Final Answer:

    User.where(active: true).pluck(:email).uniq -> Option A
  5. Quick Check:

    Filter then pluck for efficiency [OK]
Hint: Filter with where, then pluck columns, then uniq [OK]
Common Mistakes:
  • Calling where after pluck (invalid)
  • Loading objects unnecessarily before pluck
  • Not removing duplicates efficiently
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard