Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Rails Model Column Names

When one retrieves a Rails model object, how are the column (or object attribute) names accessed?

like image 664
mbm Avatar asked Mar 14 '11 15:03

mbm


People also ask

How do I get column names in an array?

Columns attribute of the dataframe returns the column labels of the dataframe. You can get the column names as an array by using the . columns. values property of the dataframe.

What is ActiveRecord in Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is ApplicationRecord in Rails?

Now ApplicationRecord will be a single point of entry for all the customizations and extensions needed for an application, instead of monkey patching ActiveRecord::Base. Say I want to add some extra functionality to Active Record. This is what I would do in Rails 4.2.

What is ActiveRecord naming convention?

Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns. Foreign keys - These fields should be named following the pattern singularized_table_name_id (e.g., item_id , order_id ).


2 Answers

User.column_names 

Like a lot of things in Rails, it just works :)

like image 121
Gregory Mostizky Avatar answered Sep 21 '22 15:09

Gregory Mostizky


Example:

user = User.find(1) p user.attributes.keys 
like image 21
Zabba Avatar answered Sep 20 '22 15:09

Zabba