Chris Kimpton
Chris Kimpton

Reputation: 5541

Rails lib class not being loaded in production, works ok in dev

I have a class in the lib directory: lib\db_cache.rb, that defines class DbCache.

My Rails model can access it when in dev mode and also when I run rails console in production mode.

But when I run the production mode rails server, the model class, eg Foo, complains about "uninitialized constant" Foo::DbCache,

org/jruby/RubyModule.java:2677:in `const_missing', 
org/jruby/RubyMethod.java:134:in `call'

I have this line in application.rb

config.autoload_paths += %W(#{Rails.root}/lib)

I have also tried the other variations shown in the linked SO questions - but no joy.

I am using jruby 1.7.3 (1.9.3p385) - Java 1.7.0_13-b20, on linux. Rails is version 3.2.12.

I've seen these questions Rails - why would a model inside RAILS_ROOT/lib not be available in production mode? and Best way to load module/class from lib folder in Rails 3? but that doesnt seem to help my case.

Thanks in advance for any ideas on this.

PS My work-hack-around for now is to require 'db_cache' in my model class :(

Upvotes: 3

Views: 700

Answers (2)

Chris Kimpton
Chris Kimpton

Reputation: 5541

The problem seemed to related to enabling config.threadsafe! in /config/environments/production.rb

This is what I had:

  # Enable threaded mode
  if defined?(Rails::Server)
    puts "Rails Server running - so enable threadsafe!"
    config.threadsafe!
  end

As I am using jruby, I dont believe this is so much of an issue. At least, when I removed these lines, things worked much better :)

Upvotes: 0

brewp
brewp

Reputation: 172

It sounds like you are trying to extend a class. Without seeing the db_cache.rb file I can't know for sure.

If that is the case it is perfectly fine to have

extend DbCache

in your model class definition

Upvotes: 1

Related Questions