0

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?
Jason Waldrip
  • 5,038
  • 8
  • 36
  • 59

3 Answers3

1

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

cpjolicoeur
  • 12,766
  • 7
  • 48
  • 59
0

To return an array from a collection:

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

An explanation of &:object

Community
  • 1
  • 1
John Paul Ashenfelter
  • 3,135
  • 1
  • 22
  • 29
0

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

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

Mike
  • 364
  • 1
  • 9