Before Rails 5, all constants were loaded via ‘autoloading’ when the application booted up. Rails 5 replaced this with ‘eager loading,’ i.e loading all the constants even before they are actually needed.
With ‘autoloading,’ the application does not load the constant until it is needed. Once a class is needed, if it is missing then the application starts looking in ‘autoload paths’ to load it. Eager_load_paths
contains a list of directories. When the application boots then it loads all constants found in all directories listed in eager_load_paths.
If you’re upgrading a legacy app from Rails version 3/4 to 5+ and you’re getting an uninitialized constant error for a module/class that is outside of your app/
directory, then look for autoload_paths in application.rb
.
We can add directories to eager_load_paths this way:
1
2
3
4
5
# config/application.rb
class Application < Rails::Application
config.eager_load_paths << Rails.root.join('lib')
end
For more info, read this commit and the official doc - happy loading!