title: Rails STI class auto initialize
body: <p>I'm trying to make a STI Base model which changes automatically to inherited class like that:</p>
  
  <pre><code>#models/source/base.rb
  class Source::Base &lt; ActiveRecord::Base
    after_initialize :detect_type
  
    private
    def detect_type
      if (/(rss)$/ ~= self.url)
        self.type = 'Source::RSS'
      end
    end
  end
  
  #models/source/rss.rb
  class Source::RSS &lt; Source::Base
    def get_content
      puts 'Got content from RSS'
    end
  end
  </code></pre>
  
  <p>And i want such behavior:</p>
  
  <pre><code>s = Source::Base.new(:url =&gt; 'http://stackoverflow.com/rss')
  s.get_content #=&gt; Got content from RSS
  
  s2 = Source::Base.first # url is also ending rss
  s2.get_content #=&gt; Got content from RSS
  </code></pre>