I realize that “lib” is no longer autoloaded by default. However, I have this in my application.rb file:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/datatables)
I have a module in lib called utility.rb, declared as follows:
module MyApp
module Utility
I have some utility methods in there, for example a method that takes an array and turns it into values that can be queried from MySQL. I have:
include MyApp
at the top of the classes that need that method, so that I can then just call:
Utility::array_to_query_string
Unfortunately, this does not work. Whether running a rake task or the application, I am met with:
uninitialized constant MyApp
I don’t know how to make Rails require other than what I have above. In the console, if I explicitly type require 'utility' and then I can successfully do the include. What do I have to do to make Rails autoload this module?
The problem could be the directory structure in your lib folder. That the rails autoloader can find your file, it needs to be placed in the right spot. Your
MyApp::Utilitymodule should live in a file called:lib/my_app/utility.rb.If you place the file directly in lib
lib/utility.rbthe autoloader won’t find it.