uninitialized constant Rails::Generators (NameError) - ruby-on-rails

I'm updating the code for a generator that I wrote, but has been working fine until now.
When I simply rue the command
bundle exec rails g
I get the following error
/Users/mpierc200/projects/prototype_conflux/vendor/gems/itrc_client_files_generator-1.0.13/lib/itrc_client_files_generator.rb:6:in `<top (required)>':
uninitialized constant Rails::Generators (NameError)
The offending line is
class ItrcClientFilesGenerator < Rails::Generators::Base
My Rails version is
Rails 3.1.9
ruby version is
ruby 1.9.3p194

It looks like the Rails generator modules were pulled out and not automatically loaded at some point during Rails 3 development. This is probably for good reasons.
You have to include them in custom generators:
require 'rails/generators'
class ItrcClientFilesGenerator < Rails::Generators::Base
# Fancy generator code here
end

Related

Uninitialized constant after upgrade rails 5.2 to 6.0

After upgrading my RoR app to 6.0, I'm getting this error:
NameError: uninitialized constant Sites::NetworkResolver::ExternalApi. This is my app/resolvers/sites/network_resolver.rb file:
module Sites
class NetworkResolver
ExternalApi::Graphql
...
end
end
and my app/services/external_api/graphql.rb
module ExternalApi
module Graphql
...
end
end
in the app/services folder, I have a settings file with the same name external_api.rb
module ExternalApi
...
end
rails zeitwerk:check
rails aborted!
NameError: uninitialized constant Sites::NetworkResolver::ExternalApi
If I put it at the top of app/resolvers/network_resolver.rb file
require 'external_api/graphql'
I need to update externalApi to externalAPI so that Api is uppercase
app/services/external_api/graphql.rb to define constant ExternalAPI::Graphql, but didn't
so code works but didn't want to update more than 100 files
By default, app/resolvers/network_resolver.rb should define NetworkResolver, rather than Sites:: NetworkResolver, maybe the path is mistaken?
You can force inflection for external_api to not use the acronym this way:
# config/initializers/zeitwerk.rb
Rails.autoloaders.main.inflector.inflect("external_api" => "ExternalApi")

Sudden "uninitialized constant PurIssue" error on STI_preload while upgrading rails to 7.0.1

I am in the process of upgrading a ~2 year old Rails app from 6.1 to 7.0.1
I have an STI setup where Pursuit is the main class and it has several other types as descendants:
# app/models/pursuit.rb
require 'sti_preload'
class Pursuit < ApplicationRecord
# ...
end
Descendant classes all look like this:
# app/models/pur_issue.rb
class PurIssue < Pursuit
# ...
end
--
# app/models/pur_goal.rb
class PurGoal < Pursuit
# ...
end
--
# app/models/pur_tracker.rb
class PurTracker < Pursuit
# ...
end
I have been using STI preload snippet as recommended by the Ruby Guides, and all worked well under Rails 6.0 and 6.1.
But now that I am upgrading to Rails 7.0.1, I suddenly get an "Uninitialized Constant" error for only the PurIssue subclass whereas all the other subclasses load fine:
Showing /Users/me/gits/rffvp/app/views/pursuits/_stats.html.slim where line #1 raised:
uninitialized constant PurIssue
Extracted source (around line #33):
33 types_in_db.each do |type|
34 logger.debug("Preloading STI type #{type}")
35 type.constantize
36 end
37 logger.debug("Types in database #{types_in_db}")
Trace of template inclusion: #<ActionView::Template app/views/layouts/_footer.html.slim locals=[]>, #<ActionView::Template app/views/layouts/application.html.slim locals=[]>
Rails.root: /Users/me/gits/rffvp
Application Trace | Framework Trace | Full Trace
lib/sti_preload.rb:33:in `block in preload_sti'
lib/sti_preload.rb:31:in `each'
lib/sti_preload.rb:31:in `preload_sti'
lib/sti_preload.rb:13:in `descendants'
app/models/pursuit.rb:71:in `<class:Pursuit>'
app/models/pursuit.rb:54:in `<main>'
app/models/pur_issue.rb:52:in `<main>'
app/views/pursuits/_stats.html.slim:1
app/views/layouts/_footer.html.slim:14
app/views/layouts/application.html.slim:13
I can't seem to figure out why PurIssue will not load anymore whereas all the other subclasses will. This is breaking my entire app since PurIssues are the most important data points.
Can someone point me to a Rails configuration change between 6.0, 6.1 and 7.0 that might cause this different behavior?
# lib/sti_preload.rb
module StiPreload
unless Rails.application.config.eager_load
extend ActiveSupport::Concern
included do
cattr_accessor :preloaded, instance_accessor: false
end
class_methods do
def descendants
preload_sti unless preloaded
super
end
# Constantizes all types present in the database. There might be more on
# disk, but that does not matter in practice as far as the STI API is
# concerned.
#
# Assumes store_full_sti_class is true, the default.
def preload_sti
types_in_db = \
base_class
.unscoped
.select(inheritance_column)
.distinct
.pluck(inheritance_column)
.compact
types_in_db.each do |type|
logger.debug("Preloading STI type #{type}")
type.constantize
end
logger.debug("Types in database #{types_in_db}")
self.preloaded = true
end
end
end
end
Zeitwerk shows the same error by the way:
$ rails zeitwerk:check
Hold on, I am eager loading the application.
rails aborted!
NameError: uninitialized constant PurIssue
/Users/me/gits/rffvp/lib/sti_preload.rb:33:in `block in preload_sti'
/Users/me/gits/rffvp/lib/sti_preload.rb:31:in `each'
/Users/me/gits/rffvp/lib/sti_preload.rb:31:in `preload_sti'
/Users/me/gits/rffvp/lib/sti_preload.rb:13:in `descendants'
/Users/me/gits/rffvp/app/models/concerns/validateable.rb:10:in `block in <module:Validateable>'
/Users/me/gits/rffvp/app/models/pursuit.rb:68:in `include'
/Users/me/gits/rffvp/app/models/pursuit.rb:68:in `<class:Pursuit>'
/Users/me/gits/rffvp/app/models/pursuit.rb:54:in `<main>'
/Users/me/gits/rffvp/app/models/pur_issue.rb:1:in `<main>'
Tasks: TOP => zeitwerk:check
(See full trace by running task with --trace)
I saw the same thing trying to do an upgrade to rails 7. I believe it originates with this change: https://github.com/rails/rails/commit/ffae3bd8d69f9ed1ae185e960d7a38ec17118a4d
Effectively the change to invoke Class.descendants directly in the internal association callback methods exposes a more longstanding implicit issue with invoking Class.descendants at all during the autoload, as the StiPreload implementation may result in a problem where it circularly attempts to constantize the original class being autoloaded. I've added an issue in the Rails repo here https://github.com/rails/rails/issues/44252

Error during testing in rails

I am trying to run a unit test.
The test needs to have require_relative 'francis'
require 'minitest/autorun'
require_relative 'francis'
class FrancisTest < MiniTest::Test
When I try running rake test TEST=test/francis_test.rb I get an error of
(in C:/Users/chris2/Documents/RailsProjects/francis)
rake aborted!
LoadError: cannot load such file -- C:/Users/chris2/Documents/RailsProjects/francis/test/francis
C:/Users/chris2/Documents/RailsProjects/francis/test/francis_test.rb:2:in `require_relative'
C:/Users/chris2/Documents/RailsProjects/francis/test/francis_test.rb:2:in `<top (required)>'
Tasks: TOP => test:single
(See full trace by running task with --trace)
How can I run this test?
Update
I moved francis_test.rb to the root of the project. I also remarked out the require_relative statement., I also had to change the setup line to #teen = ::Franci.new instead of Francis.new
So the beginning of francis_test looks like
require 'minitest/autorun'
# require_relative 'franci'
require 'test_helper'
class FrancisTest < MiniTest::Test
attr_reader :teen
def setup
#teen = ::Franci.new
end
def test_stating
assert_equal 'Whatevs.', teen.yo('Oh blah di, oh blah da.')
end
def test_yelling
assert_equal 'Chill!', teen.yo('GOOOAAAALLL!')
end
I seem to need require 'test_helper'.
BTW - my ruby and rails versions are
Rails 4.2.5.1
ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32]
Now if I run rake test TEST=francis_test.rb -v
I see (partial)
# Running:
FrancisTest#test_stating_with_acronyms = 0.01 s = E
FrancisTest#test_inquiring = 0.00 s = E
FrancisTest#test_question_with_just_numbers = 0.00 s = E
. . .
Finished in 0.013518s, 1257.5400 runs/s, 0.0000 assertions/s.
1) Error:
FrancisTest#test_stating_with_acronyms:
ArgumentError: wrong number of arguments (1 for 0)
2) Error:
FrancisTest#test_inquiring:
ArgumentError: wrong number of arguments (1 for 0)
A couple of questions
is attr_reader valid in Ruby 2.2.4? I looked at the API but couldn't quite make sense of it.
Why am I getting 'wrong number of arguments'? I haven't used the attr_reader but it seems that when calling it the way I am, it should be passing the argument to teen.yo.
BTW - the francis table only has one column - 'yo'.
require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.
For example, if you have unit test classes in the "test" directory, and data for them under the test "test/data" directory, then you might use a line like this in a test case:
require_relative "data/customer_data_1"
is attr_reader valid in Ruby 2.2.4? I looked at the API but couldn't
quite make sense of it.
Yes, it is. However you don't need it because columns are automatically made available as attributes by ActiveRecord.
Why am I getting 'wrong number of arguments'? I haven't used the
attr_reader but it seems that when calling it the way I am, it should
be passing the argument to teen.yo.
To answer this question, I need the content of your francis.rb file.

undefined method `default' for ActionMailer

I'm having this weird error with Rails 5 action mailer.
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "from#example.com"
layout 'mailer'
end
irb
~/workspace/rails5 > rails c ruby-2.4.1#rails5
Running via Spring preloader in process 45498
Loading development environment (Rails 5.1.2)
2.4.1 :001 > ApplicationMailer
NameError: uninitialized constant Mail::TestMailer
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):1
2.4.1 :002 > ApplicationMailer
NoMethodError: undefined method `default' for ApplicationMailer:Class
from app/mailers/application_mailer.rb:3:in `<class:ApplicationMailer>'
from app/mailers/application_mailer.rb:1:in `<top (required)>'
from (irb):2
2.4.1 :003 >
This is the default Scaffold of rails 5.
Two really weird things happen the first is that it always tells me the uninitialized constant then trows default is not a method right after.
Just wondering how to fix it or if I should downgrade to Rail 5.0
The solution turned out to be that if you have a model named mail the ActionMailer will grab that one first instead of the rails class. If this happens then it fails the first time but the second time will load the rails class and try and smash them together. Allowing the submodule to work but treating the main class as a monkey path.

create a new model raise error

I am working on a Rails V2.3.2 project, I wanna create a new model, so I run the following command:
rails generate model cars name:string owner:string description:text
But I got the following error:
/home/myname/.rvm/gems/ree-1.8.7-2011.03#myapp/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:55: uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
from /home/myname/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:55:in `gem_original_require'
from /home/myname/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:55:in `require'
from /home/myname/.rvm/gems/ree-1.8.7-2011.03#myapp/gems/activesupport-2.3.2/lib/active_support.rb:56
...
How to get rid of this error?
P.S.
I have the code:
require 'thread'
require File.join(File.dirname(__FILE__), 'boot')
in my RakeFile , config/engironment.rb and script/server.rb
In Rails 2, the generate command is:
ruby script/generate model car name:string owner:string description:text
rails generate is the command used in Rails 3
Also, models are singular by convention (use car instead of cars. It will avoid much head-ache afterwards).
Try to insert this code before boot.rb inside Rakefile, config/environment.rb, script/server:
require 'thread'
require File.join(File.dirname(__FILE__), 'boot')
If you still get the error above you can also add require 'thread' in your boot.rb after the RAILS_ROOT constant was defined:
RAILS_ROOT = "#{File.dirname(FILE)}/.." unless defined?(RAILS_ROOT)
require 'thread'
Also watch this post: Gem 1.5 with Rails 2.3.x

Resources