Ruby-on-rails 如何使用 rails 控制台从表中删除一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16074630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:49:39  来源:igfitidea点击:

How can i remove a column from table using rails console

ruby-on-railsactiverecordruby-on-rails-3.2rails-migrations

提问by Aman Garg

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

使用 rails 迁移可以轻松删除列。

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.

我想知道是否有任何方法可以使用控制台从表中删除列。

回答by Jun Zhou

You can run the codes in upmethod directly in rails console:

您可以up直接在方法中运行代码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:

如果您已经有一个迁移文件,例如“ db/migrate/20130418125100_remove_foo.rb”,您可以这样做:

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

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

如果你只是想做rake db:migrate,试试这个:

>> ActiveRecord::Migrator.migrate "db/migrate"