Shortcut for plucking two attributes from an ActiveRecord object?

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)