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 (Rails 4, Ruby 2)
The problem was none of these. It was actually that my Paint
model contained a field named :type
, which is a reserved keyword for meta-classing or something as such. Was thankfully helped by the kind folks at #RubyOnRails
, else a newbie like me would have tried to start from scratch, run into the same problem, and would have never have figured this out. Hopefully this helps some future Googler. Pay it forward!
Related Topics
Navigate to Ruby Function Definition in VS Code
Adding a Submit Button Image to a Rails Form
What's the Difference Between Arrays and Hashes
Rails 4 Strong Parameters:How to 'Exclude'/Blacklist Attributes Instead of Permit/Whitelist
How to Add Confirm Message with Link_To Ruby on Rails
How to Check for Stdin Input in a Ruby Script
Using Named Captures with Regex Match in Ruby's Case...When
Nameerror: Uninitialized Constant Faker
Rails, Activerecord: Self[:Attribute] VS Self.Attribute
Ruby Get Time in Given Timezone
What Is the Best Diff Library in Ruby
Verb-Agnostic Matching in Sinatra
How Would I Go About Programmatically Interacting with VSt(I) Plugins to Synthesize Audio
Set the Display Precision of a Float in Ruby