I'm trying to figure out the auto load thing, but I'm having a hard time making it to work without explicitly requiring the file.
Do I need to make some special configuration so that Rails 4 would be able to auto load files inside lib
directory?
If I go to rails console
and type $LOAD_PATH
I'm able to see that /home/some_user/workspace/rails/myapp/lib
is include into load path. This should mean that Rails will be able to auto load correct file?
So as I understand:
if I would place my files inside lib/
directory and I would use the naming conventions, Rails should be able to automatically require the correct file if anywhere in my code I would do something like this:
cats = Cats::SomeCat.new
(given that lib/cats/some_cat.rb
exists)
and the some_cat.rb
contains:
module Cats
class SomeCat
def initialize
@name = "Some cat"
end
end
end
However, Rails will show me error uninitialized constant CatController::Cats
.
If I add a line require 'cats/some_cat'
everything will work. However, in 'The Rails 4 Way' I'm reading that :
The bottom line is that you should rarely need to explicitly load Ruby code in your Rails application (using require) if you follow the naming conventions
.
Am I using the wrong naming conventions or am I forced to use that
config.autoload_paths += %W(#{config.root}/lib)
thing?