Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uninitialized Constant in Rake Tasks

Here's the setup:

New Rails app, then put this test_rake.rake in lib/tasks:

task :testclass do
  HelloClass.hello
end`

Put hello_class.rb in app/models, or in lib/ with this line: config.autoload_paths += %W(#{config.root}/lib) added to config.rb

class HelloClass
  def self.hello
    puts 'hello_class'
  end
end

rake testclass gives this error:

/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>'

Any ideas? I've uninstalled and reinstalled Ruby via RVM, deleted / rebuilt the gemset, printed out the autoload paths to make sure hello_class.rb was in one of them...

I can manually require HelloClass from within the .rake file, but then I have to do the same for anything HelloClass depends on - say, for example, if HelloClass includes HTTParty or the task sets up a delayed job.

Any help would be awesome. Thanks!

like image 541
Andrew Evans Avatar asked Dec 31 '10 22:12

Andrew Evans


2 Answers

If you start your rake task with task :testclass => :environment do, your Rails environment will be loaded and available for the task.

like image 174
Heikki Avatar answered Nov 19 '22 11:11

Heikki


This also happens when you correctly specify the dependency task :testclass => :environment do, but have enabled threadsafe!.

To fix this if you enabled threadsafe, do this:

   # in your application.rb or environment file:
   #
   config.threadsafe! unless File.split($0).last == 'rake' # unless this is a rake task
like image 35
Tilo Avatar answered Nov 19 '22 11:11

Tilo