在Rails生产环境中运行Thor

ray*_*kin 4 ruby-on-rails thor

我想在生产中的rails 3上运行一些Thor任务,但我不知道如何设置它.以下代码不起作用

class CheckData < Thor
  require File.expand_path('config/environment.rb')
end
Run Code Online (Sandbox Code Playgroud)

小智 6

将RAILS_ENV环境变量设置为require语句正上方的"生产" 应该有效.如果没有提前设置环境变量,我在这里使用条件赋值将环境默认为"生产".

class CheckData < Thor
  ENV['RAILS_ENV'] ||= 'production'
  require File.expand_path('config/environment.rb')
end
Run Code Online (Sandbox Code Playgroud)

如果从命令行将其作为Thor任务运行,则可以在运行之前设置环境变量,从而覆盖默认分配:

export RAILS_ENV=test; thor check_data
Run Code Online (Sandbox Code Playgroud)

有关更多环境变量,请参阅从RailsGuides配置Rails应用程序Rails环境设置.