mathieuprog/polymorphic_embed

Sourcing type from other column

Closed this issue · 2 comments

First time using this library, I really like it!

I have a question about type_field and the embed map. Is it possible to base the type field on another field on the record? In this way, the type would not be stored in the map at all?

Here's an example record to show what I mean:

id                           | 8f161964-55ee-48f4-8ea5-1a7c22401f28
type                         | 2000
metadata                     | {"__type__": "session_page_view", "hub_page_id": 108}

In this case, the type is an Ecto.Enum that maps to atoms. The __type__ key ends up duplicating this value onto the record. I tried type_field, but it requires the value in the embed.

Hey Stephen 👋 I need to know the type in the Ecto Type callbacks and I can't access the other fields from the schema. So no, we can't rely on another field in the schema (:type field in your example) for inferring the type. But then you may use the option to identify an embed by its specific fields if you want to get rid of the type field at all, but personally I don't like to use the latter much because it isn't reliable in case you may need to send partial data.

Thanks, that makes sense! I'm going to stick with the default behavior. I can always adjust if needed.