From the Mongoid docs, I see that if I have the following:
class Base include Mongoid::Document end class InheritedA < Base end class InheritedB < Base end
I can do the following, which will be stored with the attribute "_type".
a = InheritedA.new a.save
Mongoid will create the following document.
{ _type: "InheritedA" }
My problem is that later on I have a function that has only String _type, and I want to create the corresponding type. I tried this:
Base.new({ _type: mytype });
However, Mongoid considers this a dynamic attribute and rejects it. I know that the inclusion of dynamic attributes is not correct, because I do not want to allow this behavior in the general case.
I want to avoid having to do something like this:
ob = nil if mytype == "InheritedA" ob = InheritedA.new elsif ...
Does anyone know the correct method for this?
Erich source share