title: Rails STI conditional sub-classing from base class
body: <p>I'm developing a project where I have an entity which may have two kinds of assets: <em>Pictures</em> and <em>Videos</em>, basically. </p>
  
  <p>Since I want all the assets to be on the same table and a single upload form for either <em>Pictures</em> or <em>Videos</em>, I'm using Single Table Inheritance having both <em>Picture</em> and <em>Video</em> descending from the <em>Asset</em> class. Also, I'll be running different validations/callbacks depending on whether it is a Video or a Picture.</p>
  
  <p>I'm using paperclip to deal with the upload process, and my idea is to when uploading a file and creating an Asset with it, the application will instantiate the correct subclass (<em>Picture</em> or <em>Video</em>) depending on the mime-type of the uploaded file.</p>
  
  <p>This is a sketch of my classes:</p>
  
  <pre><code>class Project &lt; ActiveRecord::Base
    has_many :assets
    accepts_nested_attributes_for :assets
  end
  
  class Asset &lt; ActiveRecord::Base
    belongs_to :project
    has_uploaded_file :content, ...
  end
  
  class Picture &lt; Asset
    validate :image_size
    ...
  end
  
  class Video &lt; Asset
    after_save :convert_format
    ...
  end
  </code></pre>
  
  <p>My idea is to implement a <code>before_save</code> callback on the <em>Asset</em> class and try to instantiate the correct class there, but I'm not sure how to do it or if it's a good idea.</p>
  
  <p>Any idea about this?</p>