Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
Arwed Avatar asked Aug 13 '10 18:08

Arwed


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.

How do I get a list of column names in a table?

sp_columns procedure To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'

How do I get a list of column names in SQL?

In SQL Server, you can select COLUMN_NAME from INFORMATION_SCHEMA. COLUMNS .


1 Answers

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'.

like image 160
Andrea Pavoni Avatar answered Oct 10 '22 03:10

Andrea Pavoni