0

In my Rails 3 application, I've got current_account.users.

What I want is a list of the names of all the users i.e. user.full_name. What's the most elegant way to do it?

1 Answer 1

5

If full_name is a column on the model, you can use the pluck method. That would look like current_account.users.pluck(:full_name). If not, you can use map or collect (same thing), which would look like current_account.users.map(&:full_name) or current_account.users.collect(&:full_name).

Sign up to request clarification or add additional context in comments.

2 Comments

technically pluck and (map or collect) are not the same thing, as pluck selects only the column in question: current_account.users.select(:full_name).map(&:full_name) would more precisely be the same thing as pluck. If the user table has large text or binary blobs for profile information and there are many rows, this becomes a notable distinction from a performance standpoint.
I meant that collect and map are the same thing. The reason I mentioned pluck first was exactly because of that performance enhancement.

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.