Question

I'm working on upgrading a Rails 2.3.11 app to 3.0.10. I'm getting a NameError in my development.rb file, when I try to run any of the rails scripts like rails console, or run my unit tests.

I'm calling a class that I have defined in lib, but it seems that the library hasn't been loaded when development.rb calls the class.

I'm doing something like:

config.cache_store = CustomMemcachedStore.new(Memcached.new(...))

I have a file lib/custom_memcached_store.rb that declares the class

class CustomMemcachedStore < ActiveSupport::Cache::Store

I'm getting the following error:

~/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing': uninitialized constant CustomMemcachedStore (NameError)
    from ~/app_name/config/environments/development.rb:20:in `block in <top (required)>'

In application.rb, I am already using

config.autoload_paths += Dir["#{Rails.root}/lib"]

Thanks for any help you can give me.

Was it helpful?

Solution

You will need to require the file explicitly, rather than relying on autoload.

This is because the loading of the environment config happens early on in the startup process before autoload paths are set up.

In some cases you can work with initializers to insert configuration code into a place that works, via something like:

initializer "my_setup", :before => "some_other_setup" do |app|
  # ...
end

Unfortunately, this is not one of those cases, as the cache is set up here, while the autoload paths are not set up until here, immediately before the boostrap_hook.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top