0

I have these 2 files in a large system, both are located in PackA

  1. people.rb
module People
  class HobbyValueObject
  end
end
  1. job.rb
module People
  class Job
  end

  class CityValueObject
  end
end

I am trying to use CityValueObject in a different module like this,

Module is in PackB

  1. work.rb
module Profile
   class Work
       ....
       def calculateTaxes
          ...
          a = People::CityValueObject....
       end
   end
end

But it's giving me an error saying,

NameError: uninitialized constant People::CityValueObject

Did you mean? People::HobbyValueObject

Why is not being able to fine CityValueObject but can find HobbyValueObject just fine? How do I make it find the object that I am intending to use?

I am not explicitly declaring any requires or includes

5
  • 3
    Is it possible that the one file was already required and loaded, but the other file was not? Where are the files located, and what are they named? Hint: Rails autoloading only works when files and the classes inside those files follow certain naming conventions. Commented Feb 8, 2023 at 17:25
  • I edited the question with those details. I think you might be right about how autoloading only works with certain naming conventions. I might have to explicitly import the file I am trying to use. Commented Feb 8, 2023 at 17:33
  • 2
    Do you really need to define CityValueObject in job.rb? The easiest solution is to just move it into its own file. Alternatively you can just "fool" the autoloader by referencing People::Job first. Commented Feb 8, 2023 at 17:36
  • I do not have the ownership over People module so I can only work within how I can import the class that I need. Commented Feb 8, 2023 at 21:20
  • What do you mean by "PackA" and "PackB"? Are those directories or maybe gems? How are these files being loaded? Do you rely on Rails' autoloading or do you load / require them explicitly? Commented Feb 9, 2023 at 7:51

1 Answer 1

0

I was able to resolve this by adding require at the top while using full path file name.

require './packs/people/app/public/people/job'
Sign up to request clarification or add additional context in comments.

1 Comment

Using . in a require statement is pretty brittle. require_relative is better. See stackoverflow.com/a/9750732/28128

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.