Rails 3 rake 任务无法在生产中找到模型
我的简单 rake 任务存储在 lib/tasks/items_spider.rake 中,在开发中运行得很好。它所做的只是在 Item
模型上调用 spider!
。
namespace :items do
desc "Spider the web for data, hoorah"
task :spider => :environment do
Item.spider!
end
end
我有 :environment
任务作为依赖项,所以一切正常。但是,当我添加 RAILS_ENV=product
时,我在本地服务器和生产服务器上都遇到了错误:
$ rake items:spider RAILS_ENV=production --trace
(in /home/matchu/Websites/my-rails-app)
** Invoke items:spider (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute items:spider
rake aborted!
uninitialized constant Object::Item
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rspec-core-2.0.0.beta.22/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rspec-expectations-2.0.0.beta.22/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
/home/matchu/Websites/openneo-impress-items/lib/tasks/items_spider.rake:4:in `block (2 levels) in <top (required)>'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
[...trace of how rake gets to my task...]
这对我来说似乎很奇怪。显然模型没有正确加载。我使用的是 Rails 3.0.3,尽管这个应用程序的开发早在 Rails 3 处于测试阶段时就开始了。我该如何调试这个问题?谢谢!
My simple rake task, stored in lib/tasks/items_spider.rake
runs just fine in development. All it does is call spider!
on the Item
model.
namespace :items do
desc "Spider the web for data, hoorah"
task :spider => :environment do
Item.spider!
end
end
I have the :environment
task as a dependency, so everything works just fine. However, when I add RAILS_ENV=production
, I hit errors, both on my local server and the production server:
$ rake items:spider RAILS_ENV=production --trace
(in /home/matchu/Websites/my-rails-app)
** Invoke items:spider (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute items:spider
rake aborted!
uninitialized constant Object::Item
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rspec-core-2.0.0.beta.22/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rspec-expectations-2.0.0.beta.22/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
/home/matchu/Websites/openneo-impress-items/lib/tasks/items_spider.rake:4:in `block (2 levels) in <top (required)>'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3@rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
[...trace of how rake gets to my task...]
This just seems odd to me. Apparently the models have not been loaded correctly. I'm on Rails 3.0.3, though development on this app started back when Rails 3 was in beta. How can I go about debugging this issue? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与在生产中运行应用程序相反,Rake 任务不会急切地加载您的整个代码库。您可以在源中看到它:
因此,仅如果
$rails_rake_task
为false
,应用程序才会在生产环境中预先加载。并且在:environment
Rake 任务中将$rails_rake_task
设置为true
。最简单的解决方法是简单地
require
您需要的模型。但是,如果您确实需要在 Rake 任务中加载所有应用程序,则加载它非常简单:开发中所有这些工作的原因是 Rails 在开发模式下自动加载您的模型。这也适用于 Rake 任务。
Contrary to running your application in production, a Rake task does not eager load your entire code base. You can see it in the source:
So only if
$rails_rake_task
isfalse
, will the application be eager-loaded in production. And$rails_rake_task
is set totrue
in the:environment
Rake task.The easiest workaround is to simply
require
the model that you need. However, if you really need all of your application to be loaded in the Rake task, it is quite simple to load it:The reason all of this work in development is because Rails autoloads your models in development mode. This also works from within a Rake task.
在您的环境/生产.rb 中,您应该添加以下内容:
它为我解决了问题。
(注意:这应该在 config.threadsafe! 调用之后添加)
In your environment/production.rb, you should add the following:
It solved the problem for me.
(Note: this should be added AFTER the config.threadsafe! call)
在 Rails 6.0.3.2 中,同样的事情也发生在我身上。
我试图配置 rake 文件
task :foo do ... end。
相反,它应该是task foo: :environment do ... end
。The same thing happens to me in Rails 6.0.3.2.
I was trying to configure the rake file
task :foo do ... end.
Instead it should've beentask foo: :environment do ... end
.刚刚发现另一个:我正在 Windows 上开发,部署到 Heroku。 Web 应用程序和 Rails 控制台工作正常,但 rake 任务甚至直接 require 无法加载模型。结果我心不在焉地将模型文件创建为
Model.rb
而不是model.rb
- 系统依赖大小写。Just found another one: I was developing on windows, deploying to Heroku. The web app and rails console worked fine, but rake tasks and even direct require could not load the model. Turned out I had absentmindedly created the model file as
Model.rb
instead ofmodel.rb
- system dependent case sensitivity.