Rails 5: loading lib files into production

I updated one of my applications from Rails 4.2.6 to Rails 5.0.0. The Update Guide says that Autoload is disabled by default.

Now I always get an error message on my working server, since I load all the autoload lib files into the application.rb file.

 module MyApp class Application < Rails::Application config.autoload_paths += %W( lib/ ) end end 

Currently, I have set config.enable_dependency_loading to true , but I am wondering if there is a better solution for this. There must be a reason Autoloading is shutting down in production by default.

+56
ruby-on-rails ruby-on-rails-5 autoload
Jul 05 '16 at 8:30
source share
6 answers

Startup is disabled in the production environment due to thread safety. Thanks @ Green for the link.

I solved this problem by saving the lib files in the lib folder in my app directory, as recommended on Github . Each folder in the app folder is automatically loaded by Rails.

+18
Aug 18 '16 at 17:34
source share

My list of changes after upgrading to Rails 5:

  • Put lib dir in the app , because all the code inside the application is automatically loaded into dev and loaded into prod and, most importantly, automatically loaded during development, so you do not need to restart the server every time you make changes.
  • Remove all require statements that point to your own classes inside lib , because they all load automatically if the / dir file names are correct, and if you leave require statements, this may break the autoload. More here
  • Set config.eager_load = true in all environments to see problems loading the code impatiently in dev.
  • Use Rails.application.eager_load! before playing with streams to avoid circular dependency errors.
  • If you have ruby ​​/ rails extensions, leave this code in the old lib directory and load them manually from the initializer. This will ensure that the extensions are loaded to your next logic, which may depend on it:

     # config/initializers/extensions.rb Dir["#{Rails.root}/lib/ruby_ext/*.rb"].each { |file| require file } Dir["#{Rails.root}/lib/rails_ext/*.rb"].each { |file| require file } 
+80
Oct 13 '16 at 10:58
source share

There must be a reason why autoload is disabled in the default.

Here is a long discussion on this issue. https://github.com/rails/rails/issues/13142

+15
Jul 05 '16 at 8:43
source share

I just used config.eager_load_paths instead of config.autoload_paths as akostadinov mentioned about github comment: https://github.com/rails/rails/issues/13142#issuecomment-275492070

 # config.autoload_paths << "#{Rails.root}/lib" config.eager_load_paths << "#{Rails.root}/lib" 

He works in a development and production environment.

+10
Jul 08 '17 at 11:32 on
source share

For those who have struggled with this, just like me, just placing the directory under app/ not enough. Yes, you will get autoload, but not necessarily a reboot, which requires the implementation of naming conventions .

In addition, using the initializer to load the old root lib level will prevent the reboot function during development.

+1
May 10 '17 at 23:53 on
source share

Moving the lib folder to the application helped to solve the problem, my Twitter-avi did not start in production. I had a "uninitialized persistent TwitterApi" and my Twitter API was in my lib folder. I had config.autoload_paths += Dir["#{Rails.root}/app/lib"] in my .rb application, but it did not work before moving the folder.

It did the trick

0
Nov 15 '17 at 23:54 on
source share



All Articles