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.
Related
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.
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
I recently upgraded to Rails 5 and am encountering an error on development that is (what I expect, anyway) related to loading a shared module.
The error is Rspec: NameError: uninitialized constant ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES and occurs when running Rspec tests that use a method that uses a shared module.
The path for the shared module is: lib/shared/truthy.rb and the module code is below.
module Shared
module Truthy
def self.boolean(value)
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
end
end
end
The path for the file that uses the method that accesses this module is: engines/events/app/controllers/events/events_controller.rband the method that uses the shared module is below.
def official_event?
Shared::Truthy.boolean(params[:event][:official])
end
This wasn't throwing any error prior to upgrading to Rails 5, so it's safe to say there's no issue with the Rspec test.
Thanks in advance for any input.
I found out that this error is a result of Rails 5 removing uses of ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.
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')`
i'm using the resque and resque-send-later PLUGINS (not gems) in my project.
I haven't put 'require' statements anywhere in the code at all (since they're plugins and so they must be included upon initialization).
the app is working perfectly locally, but on heroku, it shows an error
"const_missing: unitialized constant User::Resque"
my User model:
class User < ActiveRecord::Base
include Resque::Plugins::SendLater
def self.testingWorker1
# code to be run in the background
end
end
my User_controller: (where i'm calling the above method from)
class UserController < ApplicationController
def testingResqueWorker
User.send_later(:testingWorker1)
end
end
so I removed the line include Resque::Plugins::SendLater from the my model
it still works perfectly locally, but now on heroku it gives an error saying "method_missing: send_later"
my question is:
1. how do we 'include' or 'require' plugins in rails? are they automatically available to all controllers and models?
2. any ideas for how to fix the above errors?
Two thoughts
any reason why you aren't using the gems?
are you sure the plugins have been added to the git repository and therefore have been deployed to heroku?