I think the issue is that you’re lacking the polymorphic hints on the associations. The following works for me:
module Product
class Base
include Mongoid::Document
embeds_many :variants, class_name: 'Variant::Base', as: :product
end
class Custom < Base
end
end
module Variant
class Base
include Mongoid::Document
embedded_in :product, polymorphic: true
field :type_id, type: String
end
class Custom < Base
end
end
product = Product::Custom.new
variant = product.variants.find_or_initialize_by(type_id: 'h', _type: 'Variant::Custom')
p product
p variant
product.save
product = Product::Base.find(product._id)
p product
p product.variants
The key is the as: :product option on embeds_many , and the polymorphic: true option on the embedded_in. Does this work for you?