Hi,
We use Mongoid v9 and embeds_many
in classes & subclasses to embed objects like this:
module Product
class Base
embeds_many :variants, class_name: "Variant::Base"
module Product
class CustomProduct < Base
module Variant
class Base
embedded_in :product, class_name: "Product::Base"
module Variant
class CustomVariant < Base
This has been working perfectly.
However, we are having trouble using find_or_initialize_by
on the embedded class
when we do this:
output_variant = product.variants.find_or_initialize_by(type_id: type_id)
we see output_variant
is class Variant::Base
. However, we can’t seem to cast it to Variant::CustomVariant
We tried
output_variant = output_variant.becomes(Variant::CustomVariant)
or
output_variant._type = "Variant::CustomVariant"
But nothing works. Any ideas?