Rails 3 rake 任务无法在生产中找到模型

发布于 2024-10-04 20:50:59 字数 1421 浏览 3 评论 0原文

我的简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的笑 2024-10-11 20:50:59

与在生产中运行应用程序相反,Rake 任务不会急切地加载您的整个代码库。您可以在中看到它:

module Rails
  class Application
    module Finisher
      # ...
      initializer :eager_load! do
        if config.cache_classes && !$rails_rake_task
          ActiveSupport.run_load_hooks(:before_eager_load, self)
          eager_load!
        end
      end
      # ...
    end
  end
end

因此,如果 $rails_rake_taskfalse,应用程序才会在生产环境中预先加载。并且在 :environment Rake 任务中将 $rails_rake_task 设置为 true

最简单的解决方法是简单地 require 您需要的模型。但是,如果您确实需要在 Rake 任务中加载所有应用程序,则加载它非常简单:

Rails.application.eager_load!

开发中所有这些工作的原因是 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:

module Rails
  class Application
    module Finisher
      # ...
      initializer :eager_load! do
        if config.cache_classes && !$rails_rake_task
          ActiveSupport.run_load_hooks(:before_eager_load, self)
          eager_load!
        end
      end
      # ...
    end
  end
end

So only if $rails_rake_task is false, will the application be eager-loaded in production. And $rails_rake_task is set to true 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:

Rails.application.eager_load!

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.

成熟的代价 2024-10-11 20:50:59

在您的环境/生产.rb 中,您应该添加以下内容:

config.dependency_loading = true if $rails_rake_task

它为我解决了问题。

(注意:这应该在 config.threadsafe! 调用之后添加)

In your environment/production.rb, you should add the following:

config.dependency_loading = true if $rails_rake_task

It solved the problem for me.

(Note: this should be added AFTER the config.threadsafe! call)

﹎☆浅夏丿初晴 2024-10-11 20:50:59

在 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 been task foo: :environment do ... end.

倥絔 2024-10-11 20:50:59

刚刚发现另一个:我正在 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 of model.rb - system dependent case sensitivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文