cjm2671
cjm2671

Reputation: 19456

How do I get an array of properties from an array of objects?

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?

Upvotes: 0

Views: 2044

Answers (1)

kddeisz
kddeisz

Reputation: 5182

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).

Upvotes: 5

Related Questions