Rails: Uninitialized Constant Just Happen on Production Server

Rails: uninitialized constant just happen on production server

Note that Rails 5 disables autoloading after booting the app in production.

From the linked blog post:

In the rare situation where our application still needs autoloading in the production environment, we can enable it by setting up enable_dependency_loading to true as follows:

# config/application.rb 

config.enable_dependency_loading = true
config.autoload_paths << Rails.root.join('lib')`

Rails console in production: NameError: uninitialized constant

I had this problem and realized it happened after I made a tweak to one of my job files.
What fixed it was restarting the spring loader. Just run

spring stop

Then the next time you run the rails console it should load things as normal.

Production uninitialized constant custom class stored in lib (heroku)

Error line says it all, you should reference class as ::BufferApp

Rails: NameError: uninitialized constant

Some things to try:

  1. Restart the rails console; changes to your models will only get picked up by a rails console that is already open if you do > reload! (although I have found this to be unpredictable), or by restarting the console.

  2. Is your model file called "phone_number.rb" and is it in "/app/models"?

  3. You should double-check the "--sandbox" option on your rails console command. AFAIK, this prevents changes. Try it without the switch.

Rails: changing from development to production leads to NameError uninitialized constant

I found the culprit - I seem to have not committed to my git repository the controller and the model files. So everything was working apart from that bit. Duh!

@rossta: thanks for helping! That stray 'a' is something that I must have added while copying the code into the SO post but your question made me look into my git repository - and this is how I found the changed but uncommitted files.

NameError - unitilaized constant - only in production

I tracked this back to a custom class that was located in the "lib" folder. Moving from Rails 4 to 5 it seems like some behaviors changed with loading custom classes which needed to be accounted for:

https://gist.github.com/maxivak/381f1e964923f1d469c8d39da8e2522f

Getting uninitialized constant error when trying to run tests

This can happen if modules are declared in a single statement when the parent module they are nested inside has not yet been loaded. I haven't looked at the code in those gems, but my hunch is that's what is happening. Chuck's solution would suggest that. calling gem 'test-unit' first will load the parent module, so the setup of zen test ends up working ok.

e.g.

module Foo::Bar
def do_stuff
# insert awesomeness here...
end
end

Will result in an error if the parent Foo module hasn't already been defined (e.g. by another gem)

A safer way to declare this is

module Foo
module Bar
def do_stuff
# insert awesomeness here...
end
end
end

May be a bug in Zentest that needs patching.



Related Topics



Leave a reply



Submit