Home > Blockchain >  Getting a NameError: uninitialized constant error upgrading to Rails 7
Getting a NameError: uninitialized constant error upgrading to Rails 7

Time:08-24

I'm getting this error

Overwrite /my_app/bin/setup? (enter "h" for help) [Ynaqdhm] n
        skip  bin/setup
       rails  active_storage:update
rails aborted!
NameError: uninitialized constant Mappings

  include Mappings::Model
          ^^^^^^^^
/my_app/config/initializers/wrap_parameters.rb:17:in `block in <main>'
/my_app/config/initializers/wrap_parameters.rb:16:in `<main>'
/my_app/config/environment.rb:5:in `<main>'
Tasks: TOP => active_storage:update => environment

while running bin/rails app:update to move from Rails 6.1 to Rails 7

The error is being caused by

ActiveSupport.on_load(:active_record) do
  include Mappings::Model
end

I tried adding require and the file name at the top of wrap_parameters and that's not working.

CodePudding user response:

This is almost certainly a zeitwerk compatibility problem, since it's now the default in Rails 7.

What it means is that the Mappings::Model class can't be found b/c it's not in the file structure that zeitwerk expects.

Suggest you start by running the zeitwerk check from the command line: bin/rails zeitwerk:check. It should find the problem for you and lead you to a solution.

If zeitwerk:check doesn't find any problems, then check the zeitwerk guide and then use the correct, zeitwerk-compliant, file name and location for the file containing the Mappings::Model class.

Probably can be fixed by putting the Mappings::Model class in app/models/mappings/model.rb

CodePudding user response:

Rails 6 issued warnings for this situation. Autoloading from initializers was deprecated, and the feature removed in Rails 7.

This is unrelated to Zeitwerk, autoloading from initializers was just wrong conceptually regardless of the autoloader.

Please see this section of the autoloading guide.

  • Related