Ruby on rails 如何使用rails控制台从表中删除列
使用rails迁移很容易删除列Ruby on rails 如何使用rails控制台从表中删除列,ruby-on-rails,activerecord,ruby-on-rails-3.2,rails-migrations,Ruby On Rails,Activerecord,Ruby On Rails 3.2,Rails Migrations,使用rails迁移很容易删除列 class SomeClass < ActiveRecord::Migration def self.up remove_column :table_name, :column_name end end class SomeClass
class SomeClass < ActiveRecord::Migration
def self.up
remove_column :table_name, :column_name
end
end
class SomeClass
我想知道是否有任何方法可以使用控制台从表中删除列。您可以直接在
rails控制台中运行up
方法中的代码:
>> ActiveRecord::Migration.remove_column :table_name, :column_name
如果您已经有一个迁移文件,如“db/migrate/20130418125100_remove_foo.rb
”,则可以执行以下操作:
>> require "db/migrate/20130418125100_remove_foo.rb"
>> RemoveFoo.up
如果您只想执行rake db:migrate
,请尝试以下操作:
>> ActiveRecord::Migrator.migrate "db/migrate"
如果我没有任何迁移文件,我想执行它。删除列:表名称,:列名称在控制台中不起作用。它说:NoMethodError:main:ObjectActiveRecord::Migration.remove\u column:table\u name,:column\u name
的未定义方法'remove\u column'!!你可能刚刚救了我的命。哈哈,好吧,这有点太夸张了,不过,谢谢你