Rails : How autoloading works

Circular dependency detected while autoloading constant


To understand this, first we need to know how rails autoload works.
First, rails provide a mechanism to let user does not to require every dependency in application files.

If we call any unloaded constant in rails, rails will try to find the file in load path and require the file by
lookup the file in load paths.

For example, a constant call Product, will lookup the product.rb file in app/models, app/controllers, lib/ and other load paths.
Rails achieve this by extend the ruby const_missing? method.

# in active_support/dependencies.rb
def const_missing(const_name)
  from_mod = anonymous? ? guess_for_anonymous(const_name) : self
  Dependencies.load_missing_constant(from_mod, const_name)




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s