ruby-on-rails – Rails Migration Change vs Up&Down方法

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails Migration Change vs Up&Down方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在阅读Rails测试处方书,在设置过程中,它要求我将迁移文件更改为以下内容
  1. class ProjectUserJoin < ActiveRecord::Migration
  2. def self.up
  3. create_table :projects_users,:force => true,:id => false do |t|
  4. t.references :project
  5. t.references :user
  6. t.timestamps
  7. end
  8. end
  9.  
  10. def self.down
  11. drop_table :projects_users
  12. end
  13. end

看起来我在Rails(4.0.0)上使用的是比书(2或3.x)更高的版本,我的迁移文件如下所示:

  1. class ProjectUserJoin < ActiveRecord::Migration
  2. def change
  3. end
  4. end

如何编辑更改方法以执行与上述上下方法相同的操作?到目前为止,我尝试使用up和down而不是self.up和self.down并使用相同的代码进行复制.那没起效.

谢谢!

解决方法

只需使用def self.up内容更改def更改.

您可以通过在控制台上运行rake db:migrate来检查结果 – 它将创建表(self.up功能)和rake db:rollback – 它将删除表(self.down功能).

猜你在找的Ruby相关文章