15

This is a follow up to this question. During development I have to restart the rails app everytime I change the code in lib/ in order for the code changes to take effect. How do I get this code to automatically reload like controllers, models, etc?

Community
  • 1
  • 1
jshen
  • 11,507
  • 7
  • 37
  • 59
  • possible duplicate of [(Rails) Reloading "lib" files without having to restart server...?](http://stackoverflow.com/questions/1114388/rails-reloading-lib-files-without-having-to-restart-server) – Madara's Ghost Jun 09 '12 at 08:18
  • possible duplicate of [Ruby on Rails 3 - Reload lib directory for each request](http://stackoverflow.com/questions/3282655/ruby-on-rails-3-reload-lib-directory-for-each-request) – shime Aug 28 '13 at 15:43

4 Answers4

3

For Rails 3, vary the instructions given in the article from @science's answer. In your environments/development.rb file, add the lines:

ActiveSupport::Dependencies.autoload_paths << File::join( Rails.root, 'lib')
ActiveSupport::Dependencies.explicitly_unloadable_constants << 'MyModuleInLibFolder'

Of course, substitute the name of your module for MyModuleInLibFolder.

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
1
module ActsAsReloadable
  def self.included(base)
    ActiveSupport::Dependencies.explicitly_unloadable_constants << base.name if Rails.env == 'development'
  end
end

To use it, simply include ActsAsReloadable in your lib/* files and add config.autoload_paths += %W(#{config.root}/lib) in config/application.rb

choonkeat
  • 5,557
  • 2
  • 26
  • 19
0

If you already did the previous approaches but doesn't works (like my case), try with config.reload_classes_only_on_change in development.rb.

Rails 4.2 here ✋

skozz
  • 2,662
  • 3
  • 26
  • 37
0

why not just enter

load Rails.root + '/lib/your_lib.rb'
A B
  • 2,013
  • 2
  • 21
  • 22