Arwed Arwed - 5 months ago 7
Ruby Question

How to get an array with column names of a table

I need an array with the column names of a table

Any ideas how I can do this with rails 3.0.0rc?

Answer

Suppose you have a Post model:

Post.column_names
# or
Post.columns.map { |column| column.name }

It will return an array with column names of the table 'posts'.