我管理一个运行旧的rails 2应用程序的服务器(很快升级到rails 4),并且有一个脚本给出错误,从控制台运行相同的代码不会。它在我运行脚本时输出:
rails@net:/c$ RAILS_ENV=production script/runner stc_cron.rb /current/vendor/rails/railties/lib/commands/runner.rb:45: /current/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:249:in `load_missing_constant': Expected /current/vendor/other_extensions/stc/app/models/contact.rb to define Contact (LoadError)
但在rails控制台中运行的完全相同的代码很好,并且/current/vendor/other_extensions/stc/app/models/contact.rb确实定义了Contact并且没有错误
这是脚本中的代码:
#!/usr/local/bin/ruby
ENV['RAILS_ENV'] = ARGV[0] || 'production'
require 'config/boot'
Extension.load_all
require 'lib/satacard'
stc=SaTaCard.new
stc.get_and_process_new_stc
stc.get_and_process_new_contacts
为什么它会在控制台中正常运行但在脚本中失败?
答案 0 :(得分:0)
看起来您没有加载Rails环境。在config / boot之后加载它:
require 'config/environment'