Posted over 1 year ago. Visible to the public.

How to debug Rails autoloading

ActiveSupport::Dependencies takes care of auto-loading any classes in development. This is usually useful, but when you run into issues with the Rails autoloader, you should take a look at what it's doing.

For me this was useful in an "exciting" case of auto-loading classes inside a thread which caused the application to stop responding.

Rails 4.x

ActiveSupport::Dependencies includes logging support. It is easy to use:

ActiveSupport::Dependencies.logger = Rails.logger

Rails 5+

Logging support was removed for Rails 5. You need to manually patch yourself into the module's code.

Here is a dirty solution that works.

ActiveSupport::Dependencies.singleton_class.prepend( do def load_missing_constant(*args) Rails.logger.debug "#{__method__}(#{', ')})" super end end)

Example output:

>> Page::Version load_missing_constant(Object, :Page) load_missing_constant(Page, :Version) => Page::Version

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Arne Hartherz
Last edit:
over 1 year ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more