0

I am including a concern in a controller:

# app/controllers/api/v2/base_controller.rb
class API::V2::BaseController < ActionController::Base
  include UserApiAccessRecorder
end
# app/controllers/concerns/user_api_access_recorder.rb
module UserApiAccessRecorder
  extend ActiveSupport::Concern

  included do
    # xxx
  end
end

These code works normally before. And after upgrading to rails 7, i got the following error:

NameError - uninitialized constant API::V2::BaseController::UserApiAccessRecorder:
app/controllers/api/v2/base_controller.rb:9:in `<class:BaseController>'

The namespace info of UserApiAccessRecorder in the above error message is also wrong, as UserApiAccessRecorder is in the top namespace. I've no idea about how to track this issue. As i know, all modules & class in app dir are autoloaded, so this error should not occur.

2
  • 2
    The error message isn't wrong - it's just confusing if you don't know how constant lookup works in Ruby. What uninitialized constant API::V2::BaseController::UserApiAccessRecorder is telling you is the current module nesting at the point where you referenced the missing constant and not where it looked for the constant. Commented Aug 27, 2024 at 9:27
  • 1
    It will go up the module nesting which is just [API::V2::BaseController] since you're declaring the namespaces wrong before going up to main. You also have an inconsistency in the naming in that you have the correct inflexion for API but not for UserApiAccessRecorder but that more just something that makes my eyelid twitch. Commented Aug 27, 2024 at 9:30

1 Answer 1

0

seems it is not able to load the concern file, hence throwing uninitialized error. May be try requiring the file in the controller and see if error goes away, this just to test if the error is really due to loading of module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.