1

While upgrading from Ruby 2.6.6 to Ruby 3.1.3 and from Rails 5.2.6 to Rails 6.0.6, I made sure that I followed and incorporated all the changes between these 2 rails versions. I am not sure why I am getting the following error.

mw@2981eebea820:/workspaces/mw$ bundle exec rspec spec
DEPRECATION WARNING: Logger don't need to call #after_initialize directly anymore. It will be deprecated without replacement in Rails 6.1. (called from new at /workspaces/mw/config/application.rb:53)

An error occurred while loading ./spec/controllers/basic_controller_spec.rb.
Failure/Error: require 'rspec/rails'

NameError:
  uninitialized constant ActiveRecord::Railtie::ActiveRecord

          include ActiveRecord::Railties::ControllerRuntime
                  ^^^^^^^^^^^^
# /usr/local/bundle/gems/activerecord-6.0.6/lib/active_record/railtie.rb:209:in `block (2 levels) in <class:Railtie>'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:51:in `each'
# /usr/local/bundle/gems/activesupport-6.0.6/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
# /usr/local/bundle/gems/actionpack-6.0.6/lib/action_controller/base.rb:269:in `<class:Base>'
# /usr/local/bundle/gems/actionpack-6.0.6/lib/action_controller/base.rb:166:in `<module:ActionController>'
# /usr/local/bundle/gems/actionpack-6.0.6/lib/action_controller/base.rb:7:in `<main>'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
# /usr/local/bundle/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# /usr/local/bundle/gems/actionview-6.0.6/lib/action_view/test_case.rb:13:in `<class:TestCase>'
# /usr/local/bundle/gems/actionview-6.0.6/lib/action_view/test_case.rb:12:in `<module:ActionView>'
# /usr/local/bundle/gems/actionview-6.0.6/lib/action_view/test_case.rb:10:in `<main>'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
# /usr/local/bundle/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
# /usr/local/bundle/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
# /usr/local/bundle/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
# /usr/local/bundle/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# ./spec/spec_helper.rb:5:in `<main>'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/bootsnap-1.15.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require'
# /usr/local/bundle/gems/polyglot-0.3.5/lib/polyglot.rb:65:in `require'
# /usr/local/bundle/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# ./spec/rails_helper.rb:10:in `<top (required)>'
# ./spec/controllers/basic_controller_spec.rb:1:in `require'
# ./spec/controllers/basic_controller_spec.rb:1:in `<top (required)>'
genonymous
  • 1,598
  • 3
  • 18
  • 27
  • This error is very strange, because the `ActiveRecord` constant is defined at the top of the file. Could it be the case the the file has been tampered with? Could you bundle open activerecord, and compare the contents with https://github.com/rails/rails/blob/v6.0.6/activerecord/lib/active_record/railtie.rb? – Xavier Noria Jan 13 '23 at 12:52
  • @XavierNoria I compared them as you suggested with a diff software. They are exactly the same. No tampering was observed. – genonymous Jan 13 '23 at 17:54
  • Since this is very, very odd, I'd not trust even our own shadow. Could you please `bundle open activerecord` and right before the `include` line insert `p ::ActiveRecord`? Does it print something? – Xavier Noria Jan 14 '23 at 12:09
  • Please stop spring if the application uses it. – Xavier Noria Jan 14 '23 at 12:09

2 Answers2

0

Did you make any huge jumps between Rubies? You should have moved to 2.7 first, then when that works okay, you go to 3.0 and so on. That was what I did after a huge jump broke and I did not even know where the problem is coming from. I rolled back and made little jumps and it all worked out. I understand this is not a code solution but I hope it helps anyway.

uzorjchibuzor
  • 182
  • 1
  • 2
  • 12
0

So, it turned out that the project did not have any dependency on the database so require "active_record/railtie" was dropped in the application.rb but there was an explicit call in the spec/rails_helper.rb that was referring to ActiveRecord - Object.send(:remove_const, :ActiveRecord). After dropping that call, it started working. I am not sure why this was working previously in the first place. Thanks for all the help!

genonymous
  • 1,598
  • 3
  • 18
  • 27