How can i remove a column from table using rails console

Ruby on-RailsActiverecordRuby on-Rails-3.2Rails Migrations

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

You can run the codes in 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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAman GargView Question on Stackoverflow
Solution 1 - Ruby on-RailsJun ZhouView Answer on Stackoverflow