How to require some lib files from anywhere

Zoz picture Zoz · Jul 30, 2013 · Viewed 42.7k times · Source

I'll explain my situation.

Here is my file tree in my rails application :

lib/my_module.rb

require 'my_module/my_file'

module My_module

end

lib/my_module/my_file.rb

class Tweetag::Collector
   (...)
end

I've made a ruby script that I've put in config/jobs/

I really don't understand how I am supposed to require the file my_file.rb in this file.

require '../../my_module/my_file.rb'

It gives me `require': cannot load such file

Same error with just require 'my_module' which is what I do in my controllers...

Someone here to explain to me ? Thanks a lot

Answer

Vecchia Spugna picture Vecchia Spugna · Jul 30, 2013

You can autoinclude everything under the lib folderand avoid these problems:

Type this your file in config/application.rb

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]