34

If I do User.all.pluck(:email) then it works fine.

But if I do

arr = Array.new
arr = User.all

and then

arr.pluck(:email)

this is raising following error

undefined method `pluck' for #<Array:0x007f4ff8daf3c8>

which means I cannot use pluck with arrays, so how can we get particular field value from an array of records in just one line like above. I don't want to loop through each record in an array.

3
  • User.all.pluck(:email) works fine? what rails version are you using? Commented Feb 20, 2014 at 7:50
  • m using Rails4 and ruby2.0.0 Commented Feb 20, 2014 at 7:51
  • everyones answer is working....so m going to vote up all but accept @apneadiving 's answer Commented Feb 20, 2014 at 7:55

4 Answers 4

72

pluck is useful to do a minimalist db query.

When you have an array, just use map:

arr.map(&:email)
Sign up to request clarification or add additional context in comments.

8 Comments

But on a really big array, you might be better off with a new DB query using pluck ...
@Jon no. Once you've data in memory, everything is faster than another db call
Perhaps. But replacing arr = User.all ; arr.map with User.pluck is far more optimal. His question is ambiguous with regards to whether altering the initial selection from the DB is possible. I just think the performance improvement of getting the DB to provide what you need rather than manipulating the data after fetching it is worth considering.
Yes, its the first part of my answer
When I was a rails novice I would not have understood the complexities of the issue based on the first line of your answer.
|
16

Use collect, it's an Array method:

arr.collect{|u| u.email}

Comments

8

pluck(:x) is the equivalent of select(:x).map(&:x) on an ActiveRecord collection.

If you have an array, Array#map and its alias Array#collect do the same job.

If you use

User.scoped.pluck(:email)

your query will be like

SELECT users.email FROM users

So, to answer the question, you can NOT use pluck on an array, pluck is an ActiveRecord::Calculations method, not an array one.

4 Comments

It's not really equivalent at all. pluck causes a modification to the SELECT portion of the DB query. The map method iterates over an Array. Whilst the products might be the same, the processes to provide them are absolutely NOT equivalent.
@Jon I say that the compination of Activerecord's select chained by map is equivalent, not map alone.
I think that it's appropriate to say that pluck is the equivalent of select and map together, as @xlembouras does. He wasn't saying it was equivalent to either of them on their own.
I just thing that for clarity to developers unfamiliar with the differences between them, it's misleading to say that they are equivalent. The mechanisms behind each option are substantially different and have real performance implications depending on your implementation.
1

Converting to array this way is a memory hogger. Instead you can use:

arr = User.scoped
arr.pluck :email

or in a more easy to read:

User.scoped.pluck :email

This will make sure that actual user objects are not loaded into memory until they are required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.