One can use column_names
to get all the columns of a table, but what if one would like to know all the values of a particular instance in ActiveRecord.
For example,
User.column_names
=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "tel", "interests", "admin_status", "created_at", "updated_at", "city_id", "profile_image_file_name", "profile_image_content_type", "profile_image_file_size", "profile_image_updated_at"]
User.first
=> #<User 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">
When first
method is called, it returns key-value pairs. What I want is to have only values, is there a single method in ActiveRecord that can do this for us?
ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code.
The active record pattern is an approach to accessing data in a database. A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending. Edit: as Mike points out, in this case ActiveRecord is a module...
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
You can also use attributes
method to get it
User.first.attributes.values
User.first.attributes
will return you hash representation of user
object
User.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 the Array
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With