use*_*621 1 sql migration ruby-on-rails heroku ruby-on-rails-3
我在Heroku上有一个应用程序,需要清理那里的数据库,再次使用新的默认行运行所有(已编辑的)迁移(在迁移中将默认行添加到表中).
我跑了
heroku run rake db:reset
Run Code Online (Sandbox Code Playgroud)
此命令清除了数据库,但未将新行添加到表中.我试图以这种方式添加新行:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
...columns definitions...
t.timestamps
end
end
def self.up
Users.new(:name => 'my name', :password => 'super-secret-pass')
end
end
Run Code Online (Sandbox Code Playgroud)
但是没有添加新用户.我错过了什么?
迁移应该具有以下之一:
change方法.up和down方法(最好是3+中的实例方法,但类方法也可以).你有一个change方法和一个self.up方法.迁移系统首先查找change:
ActiveRecord::Base.connection_pool.with_connection do |conn|
@connection = conn
if respond_to?(:change)
#...
else
time = Benchmark.measure { send(direction) }
end
@connection = nil
end
Run Code Online (Sandbox Code Playgroud)
所以你self.up永远不会跑.
两种解决方案立即出现:
up和down方法.您可能希望查看指南的" 在迁移中使用模型"部分.我可能会选择2.