Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an attribute directly from rails Class.find(:all) array of objects

I know that Person.find(:all) returns an array of Person objects but is there somehow I can just get 'name' property of all people in the Person table?

Something like

        Person.find(:all).names
like image 399
theReverseFlick Avatar asked Feb 14 '11 19:02

theReverseFlick


1 Answers

Use :select to retrieve only specific attributes.

Person.all(:select => :name)

Would give you person objects that only have the name attribute initialized. Then you can map/collect that attribute to get the array of names.

like image 113
futuremint Avatar answered Nov 16 '22 18:11

futuremint