Rails迁移更改vs Up&Down方法

use*_*706 7 ruby ruby-on-rails rails-migrations rails-activerecord

我正在阅读Rails Test Prescriptions一书,在设置过程中,它要求我将迁移文件更改为以下内容:

class ProjectUserJoin < ActiveRecord::Migration
  def self.up
    create_table :projects_users, :force => true, :id => false do |t|
      t.references :project
      t.references :user
      t.timestamps
    end
  end

  def self.down
    drop_table :projects_users
  end
end
Run Code Online (Sandbox Code Playgroud)

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

class ProjectUserJoin < ActiveRecord::Migration
  def change
  end
end
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

ale*_*lex 6

只要改变def changedef self.up内容.

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