Rake任务中未初始化的常量

时间:2010-12-31 22:06:19

标签: rake ruby-on-rails-3

以下是设置:

New 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

rake testclass给出了这个错误:

/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,删除/重建了gemset,打印出自动加载路径以确保hello_class.rb位于其中一个...

我可以在.rake文件中手动要求HelloClass,但是我必须对HelloClass所依赖的任何内容执行相同操作 - 例如,如果HelloClass包含HTTParty或任务设置了延迟作业。

任何帮助都会很棒。谢谢!

2 个答案:

答案 0 :(得分:105)

如果您使用task :testclass => :environment do启动rake任务,您的Rails环境将被加载并可用于该任务。

答案 1 :(得分:11)

当您正确指定依赖关系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