Exception: "load_missing_constant Circular dependency detected while autoloading constant" in Rails
S

1

7

I'm using Rails 4.0.2. I added sub directories (with model names) in Concern directory:

  • /app/models/concerns/company/cache_concern.rb
  • /app/models/concerns/user/cache_concern.rb
  • /app/models/concerns/document/cache_concern.rb

cache_concern.rb in company directory had following content:

module Company::CacheConcern
  included do
    ...
  end
end

In my models class I had:

class Company
    include Company::CacheConcern
...    
end

Everything was fine till I went to production. Then I got following Exception:

`load_missing_constant': Circular dependency detected while autoloading constant Company::CacheConcern (RuntimeError)

To solve my problem I Change namespace in my concern files from Company::CacheConcern to Concerns::Company::CacheConcern. This allows me to load application in production enviroment.

But now I have problem in development enviroment in concern file in line where I'm using Company class:

NoMethodError (undefined method `current_company' for Concerns::Company:Module):

So it looks like he is searching in Concern directory. In production everything is fine. To resolve this problem I could add in concern files two colons before class name to use the class from models directory.

I know production mode does not behave the same way as development, because of caching whole app in memory. I checked all similar posts. Do I need to precede class names from model directory with two colons in concern files? I would be very grateful if someone could explain me this strange situation.

Thanks

Srinagar answered 20/1, 2014 at 16:1 Comment(0)
C
2

You did this I think it is wrong: To solve my problem I Change namespace in my concern files from Company::CacheConcern to Concerns::Company::CacheConcern. This allows me to load application in production enviroment.

Instead do this

Companies::CacheConcern and rename your folder as companies/cache_concern.rb

Convertite answered 24/10, 2014 at 8:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.