This project is archived and is in readonly mode.

#859 ✓resolved
Tom Ward

Initializer should load files in parent directories before subdirectories to minimize dependency pain

Reported by Tom Ward | August 19th, 2008 @ 12:12 PM | in 2.x

The problem

I have the following files:


class Zoo < ActiveRecord::Base
  include HippoManangement


module Zoo::HippoManagement

What happens

On starting the application up in some modes (but not all), I get a dependencies error, similar to 'Expected app/models/zoo/hippo_management.rb to define Zoo::HippoManagement'. This is cause by require_dependency being called on app/models/zoo/hippo_management.rb before app/models/zoo.rb.

The code calling require_dependency is in Rails::Initializer#load_application_classes:

def load_application_classes
  if configuration.cache_classes
    configuration.eager_load_paths.each do |load_path|
      matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
      Dir.glob("#{load_path}/**/*.rb").each do |file|
        require_dependency file.sub(matcher, '\1')

The problem is that the Dir.glob enumerates app/models/zoo/hippo_mangement.rb before app/models/zoo.rb.

The solution?

One way to get around this would be to sort the globbed files before requiring them. Another (far more involved) solution would be to fix the dependencies code so it doesn't fall over in this situation.

Comments and changes to this ticket

  • Tom Ward

    Tom Ward August 19th, 2008 @ 01:29 PM

    • Tag changed from dependencies, eager_loading to dependencies, eager_loading, patch

    I've written a patch (with tests) which sorts the globbed files. Having thought about this in some depth, I'm convinced that eager loading should load parents before children.

    In my particular situation (using modules to partition behaviour) I'm aware there are alternative solutions (such as Rick's concerned_with) but I believe using modules is not only simpler but more flexible. In any case, eager loading breaks the concerned_with pattern as it forces all concerns to be loaded, whether declared or not.

  • MatthewRudy
  • Repository

    Repository August 22nd, 2008 @ 01:09 PM

    • State changed from “new” to “resolved”

    (from [89d1c77dd012f087c091e0f23874c582ea4e3703]) Initializer to sort files before eager loading. [#859 state:resolved]

    Changed Rails::Initializer to sort files before eager loading them. This ensures that any files in a parent directory will be loaded before files in a subdirectory of the 'same' name. i.e. zoo.rb will be loaded before zoo/reptile_house.rb

    Signed-off-by: Pratik Naik

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket

Referenced by