Q:

Shortcut for plucking two attributes from an ActiveRecord object?

Is there a shorter way to do the following (
@user.employees.map { |e| { id: e.id, name: e.name } }
# => [{ id: 1, name: 'Pete' }, { id: 2, name: 'Fred' }]

User has_many employees. Both classes inherit from ActiveRecord::Base.
Two things I don't like about the above

It loads employees into memory before mapping,
It's verbose (subjective I guess).

Is there a better way?

A:

UPDATE:
see @jamesharker's solution: from ActiveRecord >= 4, pluck accepts multiple arguments:
@user.employees.pluck(:id, :name)

PREVIOUS ANSWER:
for a single column in rails >= 3.2, you can do : 
@user.employees.pluck(:name)

... but as you have to pluck two attributes, you can do :
@user.employees.select([:id, :name]).map {|e| {id: e.id, name: e.name} } 
# or map &:attributes, maybe

if you really need lower-level operation, just look at the source of #pluck, that uses select_all

A:

In ActiveRecord >= 4 pluck accepts multiple arguments so this example would become:
@user.employees.pluck(:id, :name)

A:

If you are stuck with Rails 3 you can add this .pluck_all extension : http://meltingice.net/2013/06/11/pluck-multiple-columns-rails/

