0

I tried config.autoload_paths += Dir["#{config.root}/lib/**/"] and config.eager_load_paths += Dir["#{config.root}/lib/**/"] but keep getting the uninitialized constant error.

I assume this has to do with dependency_loading being disabled as config.threadsafe! is now the default in Rails 4.

So what's the proper way and thread safe way to eager/autoload stuff from your /lib directory?

Update: The /lib structure

lib/car.rb:

module Car
  CAR_TYPES = %w[volvo saab]
end

lib/car/volvo.rb:

module Car
  class Volvo
  end
end

The error thrown is NameError (uninitialized constant Car::CAR_TYPES)

Update 2: rails c:

Car.class 
=> Module
Car::CAR_TYPES
=> NameError: uninitialized constant Car::CAR_TYPES

Update 3: Interestingly enough, Car::Volvo.new works, but Car::CAR_TYPES isn't possible to reference.

randomguy
  • 11,712
  • 15
  • 66
  • 99
  • Can you elaborate on the `uninitialized constant` error? Are you sure all the conventions are followed to allow the constant to load? – gmile Nov 26 '13 at 22:32
  • @gmile: Updated with more specifics. Please note that I'm using the `rails-api`. – randomguy Nov 26 '13 at 22:38

1 Answers1

0

Whoops. My application was actually called Car as well and weirdly that namespace was already in use Car.constants => [:Application]. Changing the module Car to something else solved the problem and I was able to reference Cars::CAR_TYPES again.

Since Car was already defined (in application.rb), autoloading doesn't even try to load lib/car.rb. See https://stackoverflow.com/a/6797707/377920 for in-depth explanation.

Community
  • 1
  • 1
randomguy
  • 11,712
  • 15
  • 66
  • 99