Rake任务中未初始化的常量
这是设置:
新的Rails应用程序,然后把这个test_rake.rake在lib / tasks:
task :testclass do HelloClass.hello end`
把hello_class.rb放在app / models或者lib /中,用这行: config.autoload_paths += %W(#{config.root}/lib)
添加到config.rb
class HelloClass def self.hello puts 'hello_class' end end
耙testing类给出这个错误:
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing' /Users/name/Sites/Rails/indexapp/lib/tasks/test_class.rake:5:in `block (2 levels) in <top (required)>' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `call' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:631:in `each' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:631:in `execute' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain' /Users/name/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `each' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:1998:in `run' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/bin/rake:31:in `<top (required)>' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/bin/rake:19:in `load' /Users/name/.rvm/gems/ruby-1.9.2-p0@global/bin/rake:19:in `<main>'
有任何想法吗? 我已经卸载并通过RVM重新安装Ruby,删除/重buildgemset,打印出自动加载path,以确保hello_class.rb在其中之一…
我可以在.rake文件中手动要求HelloClass,但是我必须对HelloClass所依赖的任何内容执行相同的操作 – 例如,如果HelloClass包含HTTParty或任务设置了延迟作业。
任何帮助都是极好的。 谢谢!
如果你使用task :testclass => :environment do
开始你的Rake任务,你的Rails环境将被加载并且可用于任务。
当你正确地指定依赖项task :testclass => :environment do
时,也会发生这种情况task :testclass => :environment do
,但启用了threadsafe!
。
要解决这个问题,如果你启用threadsafe
,执行此操作:
# in your application.rb or environment file: # config.threadsafe! unless File.split($0).last == 'rake' # unless this is a rake task