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
totrue
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:
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.Is your model file called "phone_number.rb" and is it in "/app/models"?
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
Getting a Rogue Iteration from My .Each Loop
Instance_Eval Does Not Work with Do/End Block, Only with {}-Blocks
Can't Dup Nilclass on Association Methods
Bootstrap Modal in Ruby on Rails Not Working
Ruby Parenthesis Syntax Exception with I++ ++I
Using Poltergeist with a Proxy
How to 'Join' an Array Adding to the Beginning of the Resulting String the First Character to Join
Ruby Facebook Graph API Appsecret_Proof
How to Understand the #Dup and #Clone Operate on Objects Which Referencing Other Objects
Rails 404 Error for Stylesheet or JavaScript Files
Calling Instance Variables Without @
Stubbing Controller Actions in Rspec Request Specs
How to Sort So That "Vitamin B12" Is Not in Front of "Vitamin B6"
Undefined Method Error When Creating Delayed_Job Workers with Script/Delay_Job
Import CSV in Batches of Lines in Rails
How to Mix Required Argument and Optional Arguments in Ruby