Rails.application.eager_load!不在rake脚本中工作

时间:2015-07-17 16:27:04

标签: ruby-on-rails dependencies activesupport

我试图调用Rails.application.eager_load!在我的佣金任务中。 这是我得到的错误。

LoadError: No such file to load -- application_controller
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:457:in `load'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:457:in `block in load_file'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:647:in `new_constants_in'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:456:in `load_file'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:354:in `require_or_load'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:317:in `depend_on'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:233:in `require_dependency'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:473:in `block (2 levels) in eager_load!'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:472:in `each'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:472:in `block in eager_load!'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:470:in `each'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:470:in `eager_load!'
/home/jkoehms/TECC/tecc/lib/tasks/populate.rake:3:in `<top (required)>'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:659:in `block in run_tasks_blocks'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:659:in `each'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:659:in `run_tasks_blocks'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/application.rb:452:in `run_tasks_blocks'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/railties-4.2.1/lib/rails/engine.rb:453:in `load_tasks'
/home/jkoehms/TECC/tecc/Rakefile:6:in `<top (required)>'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `eval'
/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `<main>

进一步研究,我发现我的active_support / dependencies.rb文件中的autoload_paths为空。这应该由engine.rb中的:set_autoload_paths初始化程序处理。但是从不调用初始化程序。当我通过eager_load手动调用它时,错误消失了。为什么没有调用初始化程序?

更新: 当我调用Rails.initialize时!它然后工作。但这似乎是一个沉重的呼唤。有人能解释一下发生了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为你可以在这里找到关于原因的答案:Rails 3 rake task can't find model in production

我之前遇到过类似的问题:

app@LacosteDev-aws:~/app/current$ rake resque:preload QUEUE=* --trace
** Invoke resque:preload (first_time)
** Invoke resque:setup (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute resque:setup
** Execute resque:preload
rake aborted!
NameError: uninitialized constant ResourcesController
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/bundler/gems/qor-c93ebb95226f/widget/app/controllers/resources/widget_controller.rb:2:in `const_missing'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/bundler/gems/qor-c93ebb95226f/widget/app/controllers/resources/widget_controller.rb:2
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:225:in `load_dependency'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:348:in `require_or_load'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:302:in `depend_on'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:214:in `require_dependency'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/engine.rb:417:in `eager_load!'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/engine.rb:416:in `each'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/engine.rb:416:in `eager_load!'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/engine.rb:414:in `each'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/engine.rb:414:in `eager_load!'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `to_proc'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/application/railties.rb:8:in `each'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/application/railties.rb:8:in `all'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/railties-3.1.3/lib/rails/engine.rb:412:in `eager_load!'
/media/ebsdisk/app/releases/20150630071432/vendor/bundle/ruby/1.8/gems/resque-1.25.2/lib/resque/tasks.rb:61

我只需将config.dependency_loading = true if $rails_rake_task添加到config/environments/production.rb即可。 您也可以手动在Rakefile中设置$rails_rake_task = true