Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 383
Mahtin66 Avatar asked Jan 20 '14 16:01

Mahtin66


1 Answers

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

like image 124
Taimoor Changaiz Avatar answered Oct 14 '22 04:10

Taimoor Changaiz