Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a column from my Rails model?

I need to remove a few columns from my rails model which i already created and have some row entries in that model. How to do it? Any links which has details for modifying the schema in rails ? I'm using rails version 3.

like image 546
Hemanth Avatar asked Oct 16 '10 12:10

Hemanth


People also ask

How remove column from table in rails?

remove_column :table_name, :column_name, :type Removes column, also adds column back if migration is rollbacked. Note: If you skip the data_type, the migration will remove the column successfully but if you rollback the migration it will throw an error.

How do you take out a column?

Do one or more of the following: To remove a single column, select the column you want to remove, and then select Home > Remove Columns > Remove Columns. To remove several columns, select the columns by using Ctrl + Click or Shift + Click.


1 Answers

To remove a database column, you have to generate a migration:

script/rails g migration RemoveColumns 

Then in the self.up class method, remove your columns:

def self.up   remove_column :table_name, :column_name end 

You may want to add them back in the self.down class method as well:

def self.down   add_column :table_name, :column_name, :type end 

The Rails Guide for this goes into much more detail.

like image 196
Jason stewart Avatar answered Oct 09 '22 05:10

Jason stewart