Jason Waldrip
Jason Waldrip

Reputation: 5148

How to pull out attributes from a collection into an array

If I have a class that looks like below, how to I pull out just the last name from each instance a collection into an array?

class Person
  include :Mongoid::Document

  field :first_name
  field :middle_name
  field :last_name
  field :email_address

end

Person.all # What do I do after I have the collection?

Upvotes: 0

Views: 72

Answers (3)

John Paul Ashenfelter
John Paul Ashenfelter

Reputation: 3143

To return an array from a collection:

Person.where(blah).collect(&:last_name)

An explanation of &:object

Upvotes: 0

Mike
Mike

Reputation: 364

Person.where(...search...).only(:last_name)

http://mongoid.org/docs/querying/criteria.html#only

Upvotes: 0

cpjolicoeur
cpjolicoeur

Reputation: 13106

Person.all.map(&:last_name) will do it

Upvotes: 1

Related Questions