Load Rails /lib subdirectories in production
26 Nov 2021 - Tobias ErdleWhen I add code to my app which I may want to extract later, I like to place it within RAILS_ROOT/lib
inside some subdirectory like /logging
.
To get the content of those directories loaded, the config/application.rb
needs to be extended like this:
class Application < Rails::Application
...
config.eager_load_paths << Rails.root.join('/lib/logging')
end
With this setting enabled, the application will recognize the classes when loaded with config.eager_load = true
, which is used most of the time in staging and production environments.