Get columns names with ActiveRecord

two options

Model.column_names

or

Model.columns.map(&:name)

Example Model named Rabbit with columns name, age, on_facebook

Rabbit.column_names
Rabbit.columns.map(&:name)

returns

["id", "name", "age", "on_facebook", "created_at", "updated_at"] 

This is just way active record's inspect method works: it only lists the column's from the model's table. The attributes are still there though

record.blah

will return the blah attribute, even if it is from another table. You can also use

record.attributes

to get a hash with all the attributes.

However, if you have multiple columns with the same name (e.g. both tables have an id column) then active record just mashes things together, ignoring the table name.You'll have to alias the column names to make them unique.


Active Record provides a #column_names method that returns an array of column names.

Usage example: User.column_names