Rails 迁移中的方法名称
我正在使用 Rails 进行敏捷 Web 开发来了解 Rails。在前面的章节中,作者创建了脚手架,然后开始研究迁移。在他的迁移中,有“向上”和“向下”方法,而我在我的迁移中只有“更改”方法。作者使用的是Rails 3.05(或类似的东西),而我使用的是3.1,但是,我不认为这是解释,因为使用另一本书但相同版本的Rails,我记得创建了一个具有“向上”和的迁移“向下”方法...
那么,有两个问题,
a)我在迁移中具有不同方法名称的原因是什么?
b) 它会影响功能吗?
我的迁移
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string : title
t.text :description
t.string :image_url
t.decimal :price, :precision => 8, :scale => 2
t.timestamps
end
end
end
图书迁移
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title
t.text :description
t.string :image_url
t.decimal :price, :precision => 8, :scale => 2
t.timestamps
end
end
def self.down
drop_table :products
end
end
I'm using Agile Web Development with Rails to learn about Rails. In an early chapter, the author created scaffolding and then started looking at the Migration. In his Migration, there is an "up" and and "down" method, whereas I only have a "change" method in my Migration. The author is using Rails 3.05 (or something like that) and I am using 3.1, however, I don't think that's the explanation, because using another book but same version of Rails I remember creating a migration that had the "up" and "down" methods...
So, two questions,
a) What's the reason why I have different method names in my migration?
b) is it going to affect functionality?
My Migration
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string : title
t.text :description
t.string :image_url
t.decimal :price, :precision => 8, :scale => 2
t.timestamps
end
end
end
Books Migration
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title
t.text :description
t.string :image_url
t.decimal :price, :precision => 8, :scale => 2
t.timestamps
end
end
def self.down
drop_table :products
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Rails 3.1 取消了迁移的“向上”和“向下”部分。现在,它们被称为使用更改方法的“可逆迁移”。因此,您的第一个代码示例对于 Rails 3.1 是正确的,第二个代码示例对于 3.0.x 及更早版本是正确的。以下是略过此更新的 3.1 的更改说明:https://gist.github.com/958283
重要的一行:从模型和构造性迁移生成器(例如,add_name_to_users)生成的迁移文件使用可逆迁移的更改方法,而不是普通的向上和向下方法。
如果您考虑一下,此更新是有意义的...您不再需要定义“向上”数据库的所有步骤,也不再需要输入相反的相同步骤来“向下”数据库。更改方法足够智能,可以在给定一组指令的情况下来回切换。
要回答你的第二个问题,不,它不会改变迁移的工作方式。它仍然会根据您的指示更新您的数据存储,跟踪迁移等。这只是描述模型的这些更改的更有效方式。
Rails 3.1 did away with both the "up" and "down" part of migrations.Now they are called "reversible migrations" that use the change method. So your first code example is correct for Rails 3.1, second is correct for 3.0.x and earlier. Here are the change notes for 3.1 that skim over this update:https://gist.github.com/958283
The important line: Migration files generated from model and constructive migration generators (for example, add_name_to_users) use the reversible migration's change method instead of the ordinary up and down methods.
The update makes sense if you think about it... you no longer have to define all the steps to "up" your database as well as type out the same steps in reverse to "down" your database. The change method is smart enough to go back and forth given the singe set of instructions.
To answer your second question, no it won't change how the migration works. It will still update your data store per your instructions, keep track of the migration, etc. It's just a more efficient way of describing those changes to your Model.
实际上是相同的代码,只是更干燥(并且可定制性稍差)。
这里有一个很好的描述:
http:// /edgerails.info/articles/what-s-new-in-edge-rails/2011/05/06/reversible-migrations/index.html
Same code really, just more dry (and slightly less customizeable).
Theres a good description here:
http://edgerails.info/articles/what-s-new-in-edge-rails/2011/05/06/reversible-migrations/index.html