67

It is easily possible to remove a column using rails migration.

class SomeClass < ActiveRecord::Migration
  def self.up
    remove_column :table_name, :column_name
  end
end

I want to know if there is any way to remove a column from table using console.

Aman Garg
  • 4,198
  • 2
  • 21
  • 29

1 Answers1

138

You can run the code in the up method directly in rails console:

>> ActiveRecord::Migration.remove_column :table_name, :column_name

If you already have a migration file such as "db/migrate/20130418125100_remove_foo.rb", you can do this:

>> require "db/migrate/20130418125100_remove_foo.rb"
>> RemoveFoo.up

If you just want to do rake db:migrate, try this:

>> ActiveRecord::Migrator.migrate "db/migrate"
starball
  • 20,030
  • 7
  • 43
  • 238
Jun Zhou
  • 3,060
  • 1
  • 20
  • 31
  • I want to perform it if i am not having any migration file. remove_column :table_name, :column_name does not work in console. It says: NoMethodError: undefined method `remove_column' for main:Object – Aman Garg Apr 18 '13 at 05:04
  • 6
    `ActiveRecord::Migration.remove_column :table_name, :column_name` This worked great!! – Jordan Jul 25 '14 at 00:03
  • You may have just saved my life. Lol ok that's a little overly dramatic, but srsly, _thank you_ – Freedom_Ben Jul 14 '16 at 19:58