title: Problem caching Model instances on a constant in Rails
body: <p>I am using Single-Table Inheritance (STI) on one of my models for a Rails App and I am having problems storing model objects on a constant. I have isolated the problem into a sample project and submitted it to GitHub: <a href="http://github.com/fcoury/rails-sti-caching" rel="nofollow noreferrer">http://github.com/fcoury/rails-sti-caching</a></p>
  
  <p>What I am trying to do is loading a model instance (in this case a Music model, that inherits from the Media model via STI) on an initializer (in Rails' <code>/config/initializers/</code> directory) and keep it on a constant:</p>
  
  <pre><code>MUSIC_CACHE = Hash.new
  Music.all.each { |m| MUSIC_CACHE[m.id] = m }
  </code></pre>
  
  <p>And I have a sample controller that does the following:</p>
  
  <pre><code>class MusicsController &lt; ApplicationController
    def index
      require 'pp'
      pp MUSIC_CACHE
      @debug = []
      MUSIC_CACHE.each_pair do |k, v|
        music = Music.find(k)
        d "Object for Music.find(#{k})  =&gt; class: #{music.class} - class obj_id: #{music.class.object_id} - #{music.inspect}"
        d "Object for MUSIC_CACHE[#{k}] =&gt; class: #{v.class} - class obj_id: #{v.class.object_id} - #{v.inspect}"
  
        begin
          d "  - Music.is_a?(Media) =&gt; #{v.is_a?(Media)}"
          d "  - Try to call name   =&gt; #{v.name}"
        rescue
          d "*** Error raised:\n#{$!}"
        end
      end
  
      @musics = Music.all
    end
  
    def d(s)
      puts s
      @debug &lt;&lt; s
    end
  end
  </code></pre>
  
  <p>And a view to go with it:</p>
  
  <pre><code>&lt;h1 id="music"&gt;Music&lt;/h1&gt;
  
  &lt;ul&gt;
    &lt;% for m in @musics %&gt;
    &lt;li&gt;&lt;%= m.name %&gt; - &lt;%= m.file %&gt;&lt;/li&gt;
    &lt;% end %&gt;
  &lt;/ul&gt;
  
  &lt;pre&gt;&lt;%=h @debug.join("\n") %&gt;&lt;/pre&gt;
  </code></pre>
  
  <p>The first time this code runs, the output on the <code>&lt;pre&gt;</code> tag is this:</p>
  
  <pre><code>  Object for Music.find(2)  =&gt; class: Music - class obj_id: 13067420 - #&lt;Music id: 2, name: "5th Symphony", file: "5s.mp3", type: "Music", created_at: "2009-05-06 16:31:41", updated_at: "2009-05-06 16:31:41"&gt;
    Object for MUSIC_CACHE[2] =&gt; class: Music - class obj_id: 13067420 - #&lt;Music id: 2, name: "5th Symphony", file: "5s.mp3", type: "Music", created_at: "2009-05-06 16:31:41", updated_at: "2009-05-06 16:31:41"&gt;
      - Music.is_a?(Media) =&gt; true
      - Try to call name   =&gt; 5th Symphony
  </code></pre>
  
  <p>However, if I just reload the page, here's what gets outputted:</p>
  
  <pre><code>Object for Music.find(2)  =&gt; class: Music - class obj_id: 18452280 - #&lt;Music id: 2, name: "5th Symphony", file: "5s.mp3", type: "Music", created_at: "2009-05-06 16:31:41", updated_at: "2009-05-06 16:31:41"&gt;
  Object for MUSIC_CACHE[2] =&gt; class: Music - class obj_id: 13067420 - #&lt;Music id: 2, name: "5th Symphony", file: "5s.mp3", type: "Music", created_at: "2009-05-06 16:31:41", updated_at: "2009-05-06 16:31:41"&gt;
    - Music.is_a?(Media) =&gt; false
  *** Error raised:
  You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  The error occurred while evaluating nil.include?
  </code></pre>
  
  <p>Does anyone know the rationale behind this error?</p>