简体   繁体   English

Rake任务无法加载:环境正常

[英]Rake task failing to load :environment properly

I'm running a custom rake task... 我正在运行自定义rake任务......

namespace :import do

  desc "Import terms of service as HTML from stdin"
  task :terms => :environment do
    html = STDIN.read
    settings = ApplicationWideSetting.first
    settings.terms_and_conditions = html
    if settings.save
      puts "Updated terms of service"
    else
      puts "There was an error updating terms of service"
    end
  end

end

The model ApplicationWideSetting is reported as undefined when running the task in the production environment. production环境中运行任务时,模型ApplicationWideSetting报告为未定义。 However, when running the task on other environments (ie. development , staging , test .) the task runs fine. 但是,在其他环境(即developmentstagingtest )上运行任务时,任务运行正常。

Running the process in rails console, in all environments, completes ok. 在所有环境中,在rails控制台中运行该过程都可以。

Does anyone know what's going on, things I could check? 有谁知道发生了什么,我可以检查的事情?

note : I ran the task with 注意 :我运行任务

puts Rails.env 

To check the shell environment var RAILS_ENV was getting set/read correctly. 要检查shell环境var RAILS_ENV是否正确设置/读取。 I've also tried both with and without the square brackets around the :environment dependency declaration. 我也尝试过:环境依赖声明周围有和没有方括号。

additional info: Rails v3.2.14 其他信息: Rails v3.2.14

further info : I've setup a completely fresh rails app, and the script works fine in any environment. 进一步的信息 :我已经设置了一个全新的rails应用程序,该脚本在任何环境下都能正常运行。 Since the install in question is a real production environment, I'll have to setup another deploy and check it thoroughly. 由于有问题的安装是一个真实的生产环境,我将不得不设置另一个部署并彻底检查它。 More info as I find it. 更多信息,因为我找到它。

In a nutshell, Rails doesn't eager load models (or anything else) when running rake tasks on Production. 简而言之,在生产中运行rake任务时,Rails不会急于加载模型(或其他任何东西)。

The simplest way to work with a model is to require it when you begin the rake task, and it should work as expected, in this case: 使用模型的最简单方法是在开始rake任务时require它,它应该按预期工作,在这种情况下:

# explicitly require model
require 'application_wide_setting'

It's possible to eager load the entire rails app with: 可以通过以下方式加载整个rails应用程序:

Rails.application.eager_load!

However, you may have issues with some initializers (ie. devise) 但是,您可能会遇到一些初始化程序的问题(即设计)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM