Uninitialized constant error in rails5 production environment

Asked 3 months ago, Updated 3 months ago, 7 views

An initialized constant error occurs in the production environment of rails5.
There are no errors in the development environment, and I am writing to inquire about the cause.

◆ Commands

#rails restart
# bundle exec rails runner [class name]. [method name] - eproduction
initialized constant Rails::Command::RunnerCommand::[Class Name]

「 If you run this command without "-e production", it will be successful.
※ Class Name is located under lib.

◆ Application.rb Settings

config.load_defaults 5.1
config.i18n.default_locale=:ja
config.autoload_paths+=Dir ["#{config.root}/lib/**/"]
config.eager_load_paths+=Dir["#{config.root}/lib/**/"]
config.paths.add 'lib', eager_load: true

Also, please let me know if there are any other parameters you need for analysis.

ruby-on-rails

2022-09-30 14:25

2 Answers

https://github.com/rails/rails/blob/bd5eba1adff8fa72429f5889ae26097e9756ceb0/railties/lib/rails/engine/configuration.rb#L75

In rails5+, eager_load_paths and auto_load_paths are assumed to be generated from config.paths on their own.

Therefore, only config.paths.add 'lib', eager_load:true is needed, and I think it would be better to configure paths in this format only.


2022-09-30 14:25

I probably solved it myself, so I will write down the answer as a reminder.

◆ application.rb (before modification)

config.load_defaults 5.1
config.i18n.default_locale=:ja
config.autoload_paths+=Dir ["#{config.root}/lib/**/"]
config.eager_load_paths+=Dir["#{config.root}/lib/**/"]
config.paths.add 'lib', eager_load: true

◆ application.rb (modified)

config.load_defaults 5.1
config.i18n.default_locale=:ja
#config.autoload_paths+=Dir["#{config.root}/lib/**/"]
config.eager_load_paths+=Dir["#{config.root}/lib/"]
config.paths.add 'lib', eager_load: true

The following three points were implemented.
※ Comment out autoload_paths (because it is deprecated in Rails5)
https://qiita.com/joooee0000/items/3ab0f3d791e0d0beb639
※ Modify "~lib/**/" in eager_load_paths to "~lib/"
※ Reboot Production

It is not clear at this time why this worked.
If anyone knows, please write it down.


2022-09-30 14:25

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.