Circular Dependency Detected While Autoloading Constant User

Circular dependency detected while autoloading constant when loading constant

It looks like the primary issue may just be that you haven't namespaced your UsersController under the Admin namespace, here:

class UsersController < Admin::BaseController

Simple fix:

class Admin::UsersController < Admin::BaseController

However, I suggest that you also break out your namespaces out into distinct parts to save future headache. So instead of the above, do this:

# app/controllers/admin/users_controller.rb
module Admin
class UsersController < Admin::BaseController
# ...
end
end

And do the same with all other namespaced controllers, such as:

# app/controllers/admin/base_controller.rb
module Admin
class BaseController < ApplicationController
# ...
end
end

This way, as Rails is loading and autoloading and so forth it will always be sure to define the Admin module before attempting to load the classes under it. Sometimes you get unknown constant errors otherwise. The reasoning is a bit complex, but if you'd like to have a look check out this post.

UPDATE

On Rails Edge, there is now an official Guide on the topic of Auto Loading of Constants.

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

This is such a simple thing to miss so I thought I should mentioned it.

If you tried to "Auto Replace" all of your models from:

class Model < ActiveRecord::Base

to

class Model < ApplicationRecord

chances are, you probably replaced your app/models/application_record.rb file with the same, and it looks like this right now:

class ApplicationRecord < ApplicationRecord

fix it back to read:

class ApplicationRecord < ActiveRecord::Base

and you should be good to go!

How do I fix the error Circular dependency detected while autoloading constant on Heroku / Ruby

For what's it's worth for future people with this problem, I discovered that my problem was a combination of things.

For one, the reason this was functioning differently if production and development was this setting:

config.eager_load

The reason it wasn't wasn't failing for similar "setups" (i.e. User::MyModule) seems to have something to do with the order in which files are processed for eager_loading.

The problem was that the concern was seeming to get loaded before the model sometimes, and sometimes the other way around. Both versions have problems, since the Class Purchase wasn't seen as the same class (since one was a subclass and one wasn't) and wasn't reopened properly.

So I needed to change this:

module Purchase::FinalizeUtils
extend ActiveSupport::Concern
end

to this:

#models/concerns/purchase/finalize_utils.rb 
class Purchase < ActiveRecord::Base
module FinalizeUtils
extend ActiveSupport::Concern
end
end
end

This allowed the class Purchase to be reopened instead of redefined. Or something like that. Either way the above method allows me to namespace my concerns for including like this:

#models/purchase.rb
class Purchase < ActiveRecord::Base
include FinalizeUtils
end

The file isn't named to be found by lazy loading, but since the module is in the class, the lookup path finds it.

RuntimeError: Circular dependency detected while autoloading constant

This sounds a lot like an issue found recently by Xavier Noria. In a nutshell, capybara boots your application in multithreaded mode for the test even though the setting to load all app code upfront is not activated (needed because require and friends are not threadsafe)

It's been fixed for rails 4.2, on earlier versions

config.allow_concurrency = false

in test.rb should do the trick

Circular dependency detected while autoloading constant in lib directory

I have nothing to say about autoloading, the magic never worked for me and I try to avoid it wherever possible.

For the latter, the error cause is clear: you try to load PbFactory that requires Factory class to be defined (since it derives from it,) and it is not yet defined. To resolve an issue just put your

Dir["*_factory.rb"].each {|file| require_relative file }

after the Factory class declaration.

Circular dependency detected while autoloading constant Concerns:: NameOfConcern

Very strange that the following thing hasn't been mentioned anywhere in Rails documentation, but with it, my code works without any problems.

All you have to do is to replace CommentsDeletion with Concerns::CommentsDeletion. Put otherwise, you have to put Concerns before the name of your module that you would like to mix into your models later on.

Now, that's how my module residing inside concerns directory looks like:

module Concerns::CommentsDeletion
extend ActiveSupport::Concern

included do
after_save :delete_comments, if: :soft_deleted?
end

def soft_deleted?
status == 'deleted'
end

def delete_comments
comments.each &:destroy
end
end


Related Topics



Leave a reply



Submit