Rails: uninitialized constant just happen on production server - ruby-on-rails

I have a class that I put inside lib/network:
module NetworkApi
class NetworkProxy
end
end
Then in another class, I referenced this class:
network_proxy = ::NetworkApi::NetworkProxy.new(params)
Everything runs normally on my development environment, but when I deploy to the server, I get an error at the above line with the message:
NameError: uninitialized constant NetworkApi::NetworkProxy
I don't know why this strange error happens. Please tell me why.

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')`

Related

Uninitialized constant WebHook::Endpoint when running zeitwerk test

I've recently started the upgrading process of my app to use Rails 6. Some of it is very smooth, some of it not.
The problem I am facing right now is that in a factory bot we define:
factory :webhook_endpoint, class: Webhook::Endpoint do
...
end
When running rails zeitwerk:check it simply throws Uninitialized constant WebHookEndpoint.
The module and class is defined in the models folder.
I wonder I need to require or include it for it to realize it exists.

NameError (uninitialized constant Wizard)

I thought everything in the App folder autoloads. Why would I be getting an (uninitialized constant) error?
app/form_models/user.rb
module Wizard
module User
end
end
I have been following these instructions.
https://medium.com/#nicolasblanco/developing-a-wizard-or-multi-steps-forms-in-rails-d2f3b7c692ce
However, keep getting error and the blog states:
" Remember that the Rails autoloading feature will load every Ruby class inside the app folder"
According to Rails auto-loader conventions this should be located in some path ending in wizard/user.rb but it's not.
One place to put it is app/models/concerns/wizard/user.rb where it can be loaded.

NameError: uninitialized constant (rails) - Can't 'see' Models

I've just recently stopped being able to access my Models in rails console - it's like they don't exist. I can only see the Helper files, like UserHelper.rb etc. The model files have not been changed or renamed.
I've checked the solutions in other similar threads - stopping or removing spring doesn't resolve the issue - like here NameError: uninitialized constant (rails)
The only thing I've managed to do that had any effect was to change config.eager_load = false to true in config/environments/development.rb - but this setting had always been false before.
Similarly, running Rails.application.eager_load! will load all models and make them useable, but this resets after I exit/reload rails console.
I'm running Rails v 4.2.8 in the amazon cloud9 IDE

RuntimeError: can't modify frozen Array (Rollbar, Rails 5.1 upgrade)

Unable to use rspec and rollbar after upgrading to rails 5.
Create a Rails 4 app
Upgrade gemfile to use rails 5
Try adding rollbar gem/support
Standard config/environment.rb:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
Error when running rspec:
An error occurred while loading {path to specific spec file}
Failure/Error: require File.expand_path('../../config/environment', __FILE__)
RuntimeError:
can't modify frozen Array
# ./config/environment.rb:6:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
...
No examples found.
In most cases, that error is a red herring for something else.
When encountering it, don't get overwhelmed with the recurrent can't modify frozen Array error messages, and instead check the very first error that appears when running a spec.
For example:
Failure/Error: validate :uniqueness, if: 'should_be_unique?'
ArgumentError: Passing string to be evaluated in :if and :unless
conditional options is not supported. Pass a symbol for an instance
method, or a lambda, proc or block, instead.
Just to add one tip on top of Maxximo Mussini's answer.
If anyone can't find the first error on the terminal, please try to run RSpec on one file, i.e. rspec spec/models/user_spec.rb
You should be able to find the root case.
In my case, I haven't updated my local .env variables that is required by User model
Hope it helps
Debugging this is not easy but one possible solution is simple. It could be a naming conflict with Rollbar, possibly something getting monkey-patched. If you're seeing this RuntimeError but not using Rollbar, see other answer.
Add a Module ("namespace" of your choice) around your app class definition in config/application.rb.
The module won't affect much. The only difference I could find is when printing out your app it will now appear as (that's how we found the fix vs a new working app):
<MyTestAPP::Application ...> instead of <Application ...>
Change:
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
end
To:
Module MyTestApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
end
end

How do I use domain objects from middleware with cache_classes off?

In the rails development environment, cache_classes is off so you can modify code under app/ and see changes without restarting the server.
In all environments, though, middleware is only created once. So if I have middleware like this:
class MyMiddleware
def initialize(app)
#app = app
end
def call(env)
env['model'] = MyModel.first
end
end
and I do this in config/environments/development.rb:
config.cache_classes = false # the default for development
config.middleware.use MyMiddleware
then I'll always get the following error:
A copy of MyMiddleware has been removed from the module tree but is still active!
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:414:in `load_missing_constant'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:96:in `const_missing'
/Users/me/projects/my_project/lib/my_middleware.rb:8:in `call'
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/middleware_stack.rb:72:in `new'
...
The problem is that the MyMiddleware instance is created once at system load time, but the MyModel class is reloaded on each call.
I tried 'MyModel'.constantize.first to delay binding to the class until method-call-time, but that changes the problem to a new one:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.include?
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/active_record/attribute_methods.rb:142in `create_time_zone_conversion_attribute?'
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.2/active_record/attribute_methods.rb:75:in `define_attributes_methods'
...
This appears to be a Rails bug. See if you can upgrade your Rails version to 2.3.4 or 2.3.5.
I believe this is the commit that fixed the problem. Original bug report is here.
We encountered a problem similar to yours some time ago. As far as I remember this could be remedied by setting time_zone in environment.rb to :utc. It was a while ago and I don't remember exactly the config parameter name or whether it was 'UTC' or :utc. Give it a try, maybe it'll help.

Resources