newBike
newBike

Reputation: 15002

how to change the order of columns

I got the data by 'MODEL.all' command in rails console

I want to put the column 'cgi_name' in the 3rd position when I run MODEL.all in the rails console

I use the postgres for my DB

How to get it ?

enter image description here

Upvotes: 0

Views: 2411

Answers (2)

Rajesh Omanakuttan
Rajesh Omanakuttan

Reputation: 6918

Simple answer is YOU CANNOT

There is no way to re-order the column names to be displayed when you select using Model.all.

Otherwise, you can re-order this by selecting each column in the order you want.

Model.select("column1, column2, cgi_name, column4 etc..")

Hope it helps :)

Upvotes: 2

Richard Peck
Richard Peck

Reputation: 76784

To answer your question directly, you'll have to move the columns at DB level

Currently, I only know MYSQL to support this functionality:

ALTER TABLE Employees CHANGE COLUMN empName empName VARCHAR(50) AFTER department;

Postgres, to my knowledge, does not support this functionality:

Many people new to postgresql often ask if it has support for altering column positions within a table. Currently it does not; if you want to change column positions, you must either recreate the table, or add new columns and move data

In the view, you'll have to either manually display the columns, or create a helper method to cycle through them in an order of your choosing

Upvotes: 3

Related Questions