質問

I have a gem with some common models I'm attempting to integrate into two rails apps. (Note, one of these apps is Rails 2.3)

Everything appears to be working at this point, but I'm getting the

can't dup NilClass

On the second request that uses a given model.

Setting config.cache_classes = true will resolve this, but is obviously not optimal for development mode.

Googling around I found some references to the unloadable directive, so I tried to add that to my gem models:

class User < ActiveRecord::Base
  unloadable
  #stuff
end

This resulted in Rails blowing up saying it couldn't find the constant.

I really just need a way to tell the rails app to reload the gem classes while in development mode, as opposed to throwing them away like it's currently doing.

役に立ちましたか?

解決

Okay, the issue ended up being, of course, autoload paths.

Since the models have dependices that are hard to satisfy by requiring them one at a time, I had the models.rb file that looked like:

autoload :User, 'my_gem/models/user'
autoload :Role, 'my_gem_models/role'

Which meant it was manually autoloaded and not part of the Rails autoload_path paradigm. Simply pulling in ActiveSupport::Dependencies and appending the models directory to the autoload paths worked.

ActiveSupport::Dependencies.autoload_paths += Dir.glob "#{File.expand_path("../my_gem/models/", __FILE__)}/**/"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top