NameError (uninitialized constant Error) in Rails 7

1.9k Views Asked by At

I recently upgraded my rails application to rails 7 and after upgrading when I try to call the call from the lib folder I am getting this error NameError (uninitialized constant Error)

I have following file structure in the lib folder

lib
 -> a
 -> b
 -> c
 -> d
 -> error
    -> test_errors.rb

whenver I call Error::TestErrors I got this error, I guess this error is something related to the Zeitwerk

Can someone please help me with this

whenver I call Error::TestErrors I got this error ideally this should work and as it is working on the another branch

1

There are 1 best solutions below

1
Xavier Noria On

The constant Error::TestErrors was autoloaded in Rails 6.x. Therefore, lib seems to be in the autoload paths and lib/error/test_errors.rb seems to define the expected constant.

The exception that you get is NameError, therefore, this constant is not being autoloaded. If it was, you'd get Zeitwerk::NameError. This constant seems to be accessed prematurely, when autoloading is still not ready.

We do not have enough evidence, but it is very likely the problem is simply that this constant is being referenced while the application boots, perhaps in an initializer. This has been deprecated since Rails 6.0, and removed in Rails 7.0. This is a conceptual change in Rails, unrelated to Zeitwerk.

If that was right, please, have a look at this section of the upgrading guide to understand how to update the initializer.