-
Type: Epic
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: 7.0.5
-
Component/s: Attributes
-
None
-
Done
-
Discriminator Value Override
-
Major Change
We're using the Trailblazer framework with a fairly complicated data model that includes fairly deeply nested document structures. We use inheritance on nested subclasses in some situations.
We also use both Mongod (RoR) and Mongoose (NodeJS) to access our database.
So we might have a parent class name such as:
Customer:PreferredProducts::ProductSpecificDetails
and a inheriting subclasses such as:
Customer:PreferredProducts::BookSpecificDetails < Customer:PreferredProducts::ProductSpecificDetails
and
Customer:PreferredProducts::CourseSpecificDetails < Customer:PreferredProducts::ProductSpecificDetails
The issue we have is that the _type field then has to have overly long values that match the subclass names: "Customer:PreferredProducts::ProductSpecificDetails" and "Customer:PreferredProducts::CourseSpecificDetails".
This is both cumbersome and undesired, especially since the _type field will be stored for every instance of the subclass in the database. This makes database contents dependent on the app implementation technology. Having to include development stack specific _type values in our database is ugly (and offensive), especially since we use two different stacks, Mongoid/RoR and Mongoose/NodeJS as noted. If we change the class naming (which we have done) it necessitates a reload of the entire database and changes in the other stack (we use Mongoose primarily for migration of data from a legacy RDBMS).
We need to be able to do the same in Mongoid. In the example above, we would like to be able to specify that _type field values wold be "Book" and "Course" respectively for the two subclasses.
In Mongoose we can easily specify a custom _type value override for each discriminator model (RoR subclass equivalent) to accomplish this. We would like to see the same functionality available in Mongoid.
A macro like this would do the trick:
Customer:PreferredProducts::BookSpecificDetails < Customer:PreferredProducts::ProductSpecificDetails
override_inheritance_type_value "Book"
field :fld1, type: String, default: "N"
field :fld2, type: Number
etc....
Failing that, if you could point me at the appropriate areas in the Mongoid code base that deal with instantiation subclasses based on the _type field value, we'll try and put together a monkey-patch to accomplish this and donate it back.
Thanks!
- causes
-
MONGOID-5031 Document the need to preload child classes when using discriminator value override
- Closed
- is related to
-
MONGOID-4819 Add an example overriding field accessors with read/write_attribute
- Closed
- related to
-
MONGOID-4817 Inheritance: Allow specification of discriminator field (eg. other than _type)
- Released