Multiple schemas for a single embed

Hi there,

Is there any possibility to add multiple possible schemas to an embeds_many field?

I have the following schema:

schema "category_infographics" do
  ...
  embeds_many(:data, CientOneInfographicData)
  ...
end

And depending on a belongs_to association (any condition would work for my question) I want to use a different structure for the data column, let’s say CientOneInfographicData, CientTwoInfographicData, … the list is no longer than 3 possibilities.

Do you have any ideas? because I would like to take benefit of the cast and validation options from the changesets.

Seems like a link to different foreign tables would be better. You could use postgresql to share some columns between the tables (think subclassing but more like implementation inheritence) or just use conditional foreign links to link to the right foreign table based on other row data.

Or could use a map for data. :slight_smile:

1 Like

Yes, I think I am going to use map for data, and will cast and validate with changeset by defining the types of the three possible combinations in maps, so the logic of which schema to apply relays on the elixir side.

Thanks!

1 Like

I’ve had a setup for switching a map field out for a certain embed schema in a changeset. I’ve ripped it out a few days ago, as it was just a bunch of complex code and I switched to a two step validation. Validate the code of the embed first and pass the raw validated data to the changeset with the map type. This worked for me because on the frontend (phoenix) I moved both embeded data and “outer” data to the same level (not using inputs_for anymore), which could be an issue for other people though.

1 Like