Heroku数据库rake with loop

时间:2011-06-20 13:55:19

标签: sql ruby-on-rails heroku rake

我正在尝试在heroku上运行此迁移,但它会挂起循环。

puts "0"
add_column :batches, :store_id, :integer
add_column :batches, :company_id, :integer
puts "1"
for batch in Batch.all()
    puts "2"
    batch.company_id = batch.register.store.company.id.to_i
    puts "3"
    batch.store_id = batch.register.store.id.to_i
    puts "4"
    batch.save
    puts "5"
end
puts "6"

不幸的是,由于我的“看跌”没有在控制台中显示,所以我无法确切地说它是挂在哪里。我得到的最后一行是:

 -- add_column(:batches, :store_id, :integer)

我无法通过在heroku上运行循环进行任何迁移,但它们在本地工作正常,我做错了吗?

heroku日志的输出:

2011-06-20T13:58:15+00:00 app[rake.14]: Starting process with command `rake db:migrate --trace`
2011-06-20T13:58:48+00:00 heroku[web.1]: Stopping process with SIGTERM
2011-06-20T13:58:48+00:00 app[web.1]: >> Stopping ...
2011-06-20T13:58:48+00:00 heroku[web.1]: Process exited

1 个答案:

答案 0 :(得分:1)

问题可能是因为你没有包括我在下面展示的环境;你也可以在heroku控制台中运行它

In your apps shell/terminal

heroku console

然后将其作为块运行,因为heroku不会让你运行do / end blocks

Batch.all.each {|b| b.update_attributes(:company => b.register.store.company, :store => b.register.store)}

所以这应该在控制台中工作,除非其中一个字段是nill,这就是为什么你需要一个erro的rake任务。

请改为尝试:

lib/tasks/add_bathces.rake

task :add_bathces => :environment do 
  Batch.all.each do |batch|
    if batch.register.store.comapny &&  batch.register.store
        batch.update_attributes(:company => b.register.store.company, :store => b.register.store)
    else
      puts "Store or company were nill"
    end
  end  
end