mathieuprog / polymorphic_embed

Polymorphic embeds in Ecto
Apache License 2.0
341 stars 63 forks source link

`put_polymorphic_embed` Support? #34

Closed coladarci closed 3 years ago

coladarci commented 3 years ago

We are extremely excited about this library and ran into a subtle issue for our application.

While cast_polymorphic_embed works perfectly for us, we have a situation where we need to create a record out of an already cast embed. I.e we want to move the value from one persisted record to another record.

I tried, as a work around, to use Map.from_struct and then re-cast the data, but the issue here is that the Struct that is exposed doesn't have the __type__ field and our casting very much needs it as we can't infer the type since many sub-types share the same fields (don't ask :) )

I'm going to go down the path of using get_polymorphic_type/3 and re-casting, though this isn't working, either, yet for reasons I haven't yet determined.

That said, I believe the correct answer to this is to implement put_polymorphic_embed so I can just drop in an existing value, somehow, instead of having to re-cast it. What do you think?

coladarci commented 3 years ago

For what it's worth, this is what happens when I try and use PolymorphicEmbed.get_polymorphic_type

(update), I was passing in the Struct, not the Schema for the first param. This function works and is a fine workaround for now, but question above is still valid, thanks!

mathieuprog commented 3 years ago

What happens if you use put_change/3?

mathieuprog commented 3 years ago

As I don't have feedback I'm closing this for now. Do not hesitate to come back if you have time to discuss further.