在暂存环境中运行迁移和rake任务时未加载的模型

时间:2013-01-22 15:32:55

标签: ruby-on-rails rake rails-migrations

我正在尝试使用RAILS_ENV = staging bundle exec rake db:migrate --trace

在分段中运行以下迁移
class UpdateDatabaseForPayments < ActiveRecord::Migration
  def change
    # Create the plans
    Plan.create(:name => "Student")
    Plan.create(:name => "Professional")
    Plan.create(:name => "Studio")
    # create_subscription just creates a subscription the user, 
    # nothing out of the ordinary there.
    User.all.each do |u|
      u.create_subscription
    end
  end
end

迁移文件名称为20130121190404_update_database_for_payments.rb。

此操作失败,并显示以下错误:

uninitialized constant UpdateDatabaseForPayments::Plan
/home/ubuntu/apps/teamup/releases/20130121204425/db/migrate/20130121190404_update_database_for_payments.rb:4:in `change'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p327-turbo/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:407:in `block in migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:389:in `migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:528:in `migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:777:in `call'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:777:in `ddl_transaction'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:719:in `block in migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `each'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:570:in `up'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:551:in `migrate'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:228:in `block in execute'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:223:in `each'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:223:in `execute'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:166:in `block in invoke_with_call_chain'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-p327-turbo/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:159:in `invoke_with_call_chain'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:152:in `invoke'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:143:in `invoke_task'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:101:in `each'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:101:in `block in top_level'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:110:in `run_with_threads'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:95:in `top_level'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:73:in `block in run'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:160:in `standard_exception_handling'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/lib/rake/application.rb:70:in `run'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/gems/rake-10.0.3/bin/rake:33:in `<top (required)>'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/bin/rake:23:in `load'
/home/ubuntu/apps/teamup/shared/bundle/ruby/1.9.1/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

现在,奇怪的是

  • 此迁移在我的暂存的本地副本上运行正常 环境
  • 我可以毫无问题地通过rails控制台创建计划。

我尝试过使用文件的名称,但无论我做什么都是同样的错误。

3 个答案:

答案 0 :(得分:1)

部署包含Plan模型的代码。正如错误所示,它无法找到Plan模型。

编辑:

要证明确实是这种情况,您可以将以下内容置于失败迁移的顶部:

Rails.application.eager_load!
ActiveRecord::Base.descendants.each {|d| puts d.name}

这将打印一个模型列表,基本上是Rails知道的。我预测你不会在其中找到Plan。为了满足我的好奇心,您还可以提出:puts Dir.glob("app/models/*.rb").map{|p| p.sub("app/models/","")}

答案 1 :(得分:1)

因此,我们发现问题在于我们在分段中启用了线程安全,因为它将成为Rails 4中的标准。 显然自动加载不是线程安全的,这就是未加载模型的原因。 The error is also discussed here. 修复是设置以下

config.threadsafe! unless $rails_rake_task

Moncef Belyamani here所示。

也可以通过在迁移,种子文件或其他任何失败的rake任务中明确要求您的模型来修复它。

答案 2 :(得分:0)

对不起,我发不出来了。

enter image description here

迁移用于更改数据库架构。数据应该在seeds.rb。

中创建