title: Instantiate mongoid subclasses by _type field
body: <p>From the Mongoid docs I see that if I have the following:</p>
  
  <pre><code>class Base
    include Mongoid::Document
  end
  class InheritedA &lt; Base
  end
  class InheritedB &lt; Base
  end
  </code></pre>
  
  <p>I can do the following, which will get stored with the "_type" attribute.</p>
  
  <pre><code>a = InheritedA.new
  a.save
  </code></pre>
  
  <p>Mongoid will create the following document.</p>
  
  <pre><code>{ _type: "InheritedA" }
  </code></pre>
  
  <p>My problem is that later on I have a function which has only the String _type value and I wish to instantiate the proper type. I have tried this:</p>
  
  <pre><code>Base.new({ _type: mytype });
  </code></pre>
  
  <p>However, Mongoid thinks this is a dynamic attribute and rejects it. I know turning on dynamic attributes is not the proper course because I don't want to allow that behavior in the general case.</p>
  
  <p>I want to avoid having to do something like this:</p>
  
  <pre><code>ob = nil
  if mytype == "InheritedA"
    ob = InheritedA.new
  elsif
      ...
  </code></pre>
  
  <p>Does anyone know the proper method for accomplishing this?</p>