Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload rails initializers

In application.rb, I have

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

So when I modify a class under lib, my code is reloaded.

However, when I tried adding config/initializers to autoload, I noticed my code doesn't get updated.

If I'm writing an extension for the string class, I have to restart rails every time I modify my code.

Please advise?

like image 243
Abdo Avatar asked Jun 15 '12 09:06

Abdo


2 Answers

Initializer files are loaded only once when the rails server is started. Restart the server when initialzers values are changed. For further information see the rails initialization guides.

Auto Reloading 'lib' on change

You can auto reload lib files. Follow Link Autoload and Reload lib directory on change

In Configuring Rails Applications: config.reload_classes_only_on_change enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true. If config.cache_classes is true, this option is ignored.

like image 43
Ganesh Arulanantham Avatar answered Nov 20 '22 18:11

Ganesh Arulanantham


Initializers are only loaded when starting rails (and never reloaded). When tinkering in config/initializers you will have to restart rails every time.

Of course, you could make sure your code is defined in /lib so you can still make sure it works, by using your test-suite.

E.g. in lib/speaker.rb write

module Speaker
  def speak
    puts "Ahum, listen: #{self.to_s}"
  end
end

and in your initializer you could then do something like

class String
  include Speaker
end

While this will still only get loaded when starting rails, you can develop and test your module more easily.

Hope this helps.

like image 129
nathanvda Avatar answered Nov 20 '22 18:11

nathanvda