5

Every time I try to run any class from my rails 2.2 app's lib directory using "script/runner -e production ClassName.run" I get the following error:

/usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/runner.rb:47:  
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:89:in `const_missing': uninitialized constant ClassName (NameError)"

For some reason I don't get this error when I run it as test instead of production, and everything works fine.

O yeah run is a class method i.e. def self.run .... end

Any suggestions?

2 Answers 2

10

That error occurs when ruby can't find a Class or Module. I'd start out by:

  1. Checking gem dependencies (are they same for all environments?)
  2. Search your code for anything that defines ClassName, particularly Modules, Classes and plugins.
  3. disable each of your plugins in dev, does any plugin suddenly cause that error?
  4. if the code is in a lib add the lib require statement to your production.rb to force the lib to be loaded in production.

Hope that helps.

update Just to summarise the comments it was option 4.

Unless you only want to load the lib in production you should think about making sure all environments load the lib by doing one of the following:

  1. Create a rails initializer (a .rb file under config/initializers) with the require in it
  2. Add the path to the lib in config/environment.rb by enabling and modifying the config.load_paths variable.
3
  • That's the thing. The missing class in question is one that I've defined in lib. Even simple stuff like matching the classname to the file name checks out; in short it's defined. I don't know what I'm missing since it runs great on dev and test just not production Feb 24, 2009 at 20:18
  • Ah, that's interesting... OK one thing about moving to production is that things are initialised and cached in different ways to dev and test. Where does the lib get called from? Particularly see if requiring the lib in production.rb solves the issue. Feb 24, 2009 at 22:26
  • I just call it from my app's root... I can't wait to test this out tonight; OMG if this is it that would be awesome Feb 24, 2009 at 22:59
1

I had multiple class definition(all STI classes) on the same file. and when I separated the definition into their respective files, It works

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.