如何将集合中的属性拉出到数组中

时间:2012-03-29 18:25:30

标签: ruby ruby-on-rails-3 mongoid

如果我有一个如下所示的类,如何将每个实例的姓氏从一个集合中拉出一个数组?

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?

3 个答案:

答案 0 :(得分:1)

Person.all.map(&:last_name)会这样做

答案 1 :(得分:0)

从集合中返回数组:

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

An explanation of &:object

答案 2 :(得分:0)

Person.where(...搜索...)仅。(:姓氏)

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