rails3.1 autoloading failure
I am trying to add a module to my Rails 3.1 app, I've been able to do this before, but it is not working now with the latest module I've added. Any thoughts greatly appreciated
in application.rb
# Custom director开发者_如何学运维ies with classes and modules you want to be autoloadable. config.autoload_paths += %W(#{Rails.root}/app/workers #{Rails.root}/lib/validators #{Rails.root}/lib/content_items #{Rails.root}/lib/booher_modules )
in lib/booher_modules/mongoid_counter_cache.rb
module Mongoid module CounterCache extend ActiveSupport::Concern module ClassMethods def counter_cache(options) ... some stuff ...
Now vote.rb:
class Vote include Mongoid::Document include Mongoid::Timestamps include Mongoid::CounterCache
Anytime I try to boot up the application, I get the uninitialized constant error:
Users/Tim/Sites/polco/app/models/vote.rb:4:in `': uninitialized constant Mongoid::CounterCache (NameError) from /Users/Tim/Sites/polco/app/models/vote.rb:1:in `' from /Users/Tim/.rvm/gems/ruby-1.9.2-p290@cba/bundler/gems/mongoid-ccae125ccfd8/lib/rails/mongoid.rb:66:in `load_model' ... so on
I tried to put require 'lib/mongoid_counter_cache.rb' in vote.rb, but I get:
rails c /Users/Tim/.rvm/gems/ruby-1.9.2-p290@cba/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:306:in `rescue in depend_on': No such file to load -- lib/mongoid_counter_cache (LoadError)
You're having this issue because Rails is trying to include "Mongoid::CounterCache".
To do this, it is looking for the file "mongoid/counter_cache.rb" somewhere in the autoload path.
So...
...
lib/booher_modules/mongoid/counter_cache.rb
...
Thus to fix...
mkdir -p lib/booher_modules/mongoid
mv lib/booher_modules/mongoid_counter_cache.rb lib/booher_modules/mongoid/counter_cache.rb
The reason your specific "require 'lib/mongoid_counter_cache.rb'" doesn't work is because that doesn't look in the autoload path it looks in the main include path ($:) which doesn't include lib/booher_modules (only autoload is configured with that)
精彩评论