title: Load Subclases on Rails Initialization
body: <p>I am having problems with getting Rails (3.1.3) to load some of my ActiveRecord subclasses on application initialization. Here is the directory structure I have:</p>
  
  <pre><code>- models
    - class1.rb # Class1 &lt; ActiveRecord::Base
    - class1s
      - subclass1.rb # Subclass1 &lt; Class1
      - subclass2.rb # Subclass2 &lt; Class1
  </code></pre>
  
  <p>I am trying to make use of <code>Class1.subclasses</code>, but it keeps returning <code>[]</code>. Adding <code>#{config.root}/app/models/class1s</code> to <code>config.autoload_paths</code> doesn't help because those clases are lazy loaded and nothing references the subclasses before I make a call to <code>subclasses</code>, so they are loaded yet. Adding the path to <code>config.eager_load_paths</code> doesn't seem to work either, and its behavior is based off the <code>config.cache_classes</code> setting which is usually different in development and production.</p>
  
  <p>I'd like to be able to start up the rails console and see:</p>
  
  <pre><code>&gt; Class1.subclasses
   =&gt; [Subclass1, Subclass2]
  </code></pre>
  
  <p>Right now this is the behavior I see:</p>
  
  <pre><code>&gt; Class1.subclasses
   =&gt; []
  &gt; Subclass1
   =&gt; Subclass1
  &gt; Class1.subclasses
   =&gt; [Subclass1]
  &gt; Subclass2
   =&gt; Subclass2
  &gt; Class1.subclasses
   =&gt; [Subclass1, Subclass2]
  </code></pre>
  
  <p>Right now I am reduced to putting this at the bottom of my Class1 definition to get all of the subclasses to load on when I access the Class1 class:</p>
  
  <pre><code>ruby_files_pattern = File.join(Rails.application.config.root, "app", "models", "class1s", "**", "*.rb")
  Dir.glob(ruby_files_pattern).each do |file|
    ActiveSupport::Dependencies.require_or_load(file)
  end
  </code></pre>
  
  <p>It gets the job done, but I feel dirty doing it. Any thoughts would be much appreciated.</p>