How to get all values of an object in ActiveRecord in Rails?
You can also use attributes
method to get it
User.first.attributes.values
User.first.attributes
will return you hash representation ofuser
objectUser.first.attributes { "id" => 1, "email" => "[email protected]", "tel" => "+923223333333", "interests" => "Hi, I'm interested in coding.", "admin_status" => "download", "created_at" => "2016-08-16 22:38:05", "updated_at" => "2016-08-16 22:38:05", "city_id" => 2, "profile_image_file_name" => "Screen_Shot_2016-07-02_at_4.41.32_PM.png", "profile_image_content_type" => "image/png", "profile_image_file_size" => 324372, "profile_image_updated_at" => "2016-08-16 22:38:04" }
values
will return you the values in theArray
User.first.attributes.values [1, "[email protected]", "+923223333333", "Hi, I'm interested in coding.", "download", "2016-08-16 22:38:05", "2016-08-16 22:38:05", 2, "Screen_Shot_2016-07-02_at_4.41.32_PM.png", "image/png", 324372, "2016-08-16 22:38:04"]
I'm extending @deepak's answer to assure that the order remains same as it is for column_names
, and this is how, I achieved it:
User.first.attributes.values_at *User.column_names
To get just the values, irrespective of the order:
User.first.attributes.values
Just you can convert activerecord to Hash then get the values as the following:
User.first.as_json.values