5

Rails 3.2.3. I have a Rails app with /lib in in my autoload paths. One of my library files is requiring a bunch of files like so

Dir[Rails.root.join("lib/foo/*.rb")].each { |f| require f }

In development, those files don't reload if I change them. For example, if I modify /lib/foo/bar.rb, I will have to restart the server to see those changes. Can anyone suggest a way so that on each request they are reloaded properly?

axsuul
  • 7,370
  • 9
  • 54
  • 71

3 Answers3

3

If you have those files in your autoload path:

config.autoload_paths += %W(#{config.root}/lib)

then you shouldn't need to require them. Have you tried with this?

Community
  • 1
  • 1
Fran
  • 1,073
  • 10
  • 19
3

require doesn't play very nicely with rails' autoloading system.

require_dependency will do pretty much what require does but keeps the autoloading system in the loop so that the loaded constants will get unloaded at the end of the request. You may need to ensure that the files are in rails' autoload paths

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • Once I removed `require` reloading happened automatically if module file changed. Thanks @Frederick Cheung – W.M. Jun 19 '16 at 15:42
0

You have to add or un comment the line.

config.autoload_paths += %W(#{config.root}/lib)

to your Application class in config/application.rb

urjit on rails
  • 1,763
  • 4
  • 19
  • 36