Ruby-on-rails 重命名导轨中的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4631630/
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
Renaming table in rails
提问by Tommy
I want to rename a table... (any table.)
我想重命名一个表...(任何表。)
I tried this line of code:
我试过这行代码:
ActiveRecord::ConnectionAdapters::SchemaStatements.rename_table(old_name, new_name)
Here's the weird thing. I know I got it working the first time, but now I get this error: undefined method `rename_table' for ActiveRecord::ConnectionAdapters::SchemaStatements:Module
这是奇怪的事情。我知道我第一次让它工作,但现在我收到这个错误:未定义方法`rename_table' for ActiveRecord::ConnectionAdapters::SchemaStatements:Module
Was there something I need to set?
有什么我需要设置的吗?
回答by Mikhail Grishko
Remember that in Rails >= 3.1 you can use the changemethod.
请记住,在 Rails >= 3.1 中,您可以使用该change方法。
class RenameOldTableToNewTable < ActiveRecord::Migration
def change
rename_table :old_table_name, :new_table_name
end
end
回答by cam
You would typically do this sort of thing in a migration:
你通常会在迁移中做这样的事情:
class RenameFoo < ActiveRecord::Migration
def self.up
rename_table :foo, :bar
end
def self.down
rename_table :bar, :foo
end
end
回答by vonconrad
.rename_tableis an instance method, not a class method, so calling Class.methodisn't going to work. Instead you'll have to create an instance of the class, and call the method on the instance, like this: Class.new.method.
.rename_table是一个实例方法,而不是一个类方法,所以调用Class.method是行不通的。相反,你必须要在实例创建类的实例,并调用该方法,像这样:Class.new.method。
[EDIT]
In this instance, ActiveRecord::ConnectionAdapters::SchemaStatementsisn't even a class (as pointed out by cam), which means that you can't even create an instance of it as per what I said above. And even if you used cam's example of class Foo; include ActiveRecord::ConnectionAdapters::SchemaStatements; def bar; rename_table; end; end;, it still wouldn't work as rename_tableraises an exception.
[编辑] 在这种情况下,ActiveRecord::ConnectionAdapters::SchemaStatements甚至不是一个类(如 cam 所指出的),这意味着您甚至无法按照我上面所说的那样创建它的实例。即使您使用了 cam 的示例class Foo; include ActiveRecord::ConnectionAdapters::SchemaStatements; def bar; rename_table; end; end;,它仍然不会在rename_table引发异常时起作用。
On the other hand, ActiveRecord::ConnectionAdapters::MysqlAdapterisa class, and it is likely this class you'd have to use to rename your table (or SQLite or PostgreSQL, depending on what database you're using). Now, as it happens, ActiveRecord::ConnectionAdapters::MysqlAdapteris already accessible through Model.connection, so you should be completely able to do Model.connection.rename_table, using any model in your application.
[/EDIT]
另一方面,ActiveRecord::ConnectionAdapters::MysqlAdapter是一个类,很可能您必须使用这个类来重命名您的表(或 SQLite 或 PostgreSQL,具体取决于您使用的数据库)。现在,碰巧的ActiveRecord::ConnectionAdapters::MysqlAdapter是,已经可以通过 访问Model.connection,因此您应该完全能够Model.connection.rename_table使用应用程序中的任何模型来执行此操作。[/编辑]
However, if you wish to permanently rename a table, I would suggest using a migration to do it. It's easy and the preferred way of manipulating your database structure with Rails. Here's how to do it:
但是,如果您希望永久重命名表,我建议使用迁移来完成。这是使用 Rails 操作数据库结构的简单和首选方式。这是如何做到的:
# Commandline
rails generate migration rename_my_table
# In db/migrate/[timestamp]_rename_my_table.rb:
class RenameMyTable < ActiveRecord::Migration
def self.up
rename_table :my_table, :my_new_table
end
def self.down
rename_table :my_new_table, :my_table
end
end
Then, you can run your migration with rake db:migrate(which calls the self.upmethod), and use rake db:rollback(which calls self.down) to undo the migration.
然后,您可以使用rake db:migrate(调用self.up方法)运行迁移,并使用rake db:rollback(调用方法self.down)撤消迁移。
回答by imsinu9
ActiveRecord::Migration.rename_table(:old_table_name, :new_table_name)

