0

I have a basic User model in app/models/user.rb. I also have a few services in lib. For example, I have lib/services/user/creation_service.rb. The following code generates an error:

# lib/services/user/creation_service.rb

module Services
  module User
    class CreationService
      ...
      def create_new_user
        # User.new below causes an error because it defaults to Services::User which is a module instead of User which is an ActiveRecord class
        User.new 
        ...
      end
      ...
    end
  end
end

Is there any way to get User.new to refer to app/models/user.rb instead of the Services::User module in the code above?

Any help would be greatly appreciated!

1
  • 5
    ::User.new would do the trick Commented Aug 11, 2021 at 16:03

1 Answer 1

1

Use ::User.new instead of User.new. The double colon basically tells ruby to look for the constant that does not have a parent.

So in this case ::User will point to User which is a class instead of Services::User which is a 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.