views:

185

answers:

2

Hi -

I have a class located inside RAILS_ROOT/lib folder, which I use in one of my helpers, and it works great in development.

When I switch to production, the application throws a NameError (uninitialized constant SomeHelper::SomeClass), and I have to load it manually in the helper:

load "#{Rails.root}/lib/some_class.rb"

module SomeHelper
  def some_method
    sc = SomeClass.new
    # blah
  end
end

I was under the impression that everything inside RAILS_ROOT/lib/* should be available all to the app - is there anything I need to configure to make this happen in prod mode? thanks.

+1  A: 

When you call SomeHelper::SomeClass, Rails' autoloading mechanism will try to load file at lib/some_helper/some_class.rb

Rails won't load everything in lib/*, it will only try to load files when ConstMissing occurs.

Vojto
+1  A: 

You might need to check differences between the configuration settings between development and production environment: config/environments/production.rb and config/environments/development.rb.

During the Rails initialization routine, load_plugins() is called which loads all plugins in config.plugin_paths. You need to make sure that your folder lib/ is included, like in

config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]

In addition to config.plugin_paths, you can also name the plugins that should be loaded in config.plugins. If that variable contains :all then all plugins (found) will be loaded.

(By the way: configuration settings equal to either environment should go in config/environment.rb. Any differences between enviroments are due to settings in the respective .rb files.)

jschulenklopper