mogadget

mogadget

How to use embeds_many with schemaless changeset?

I have this schemaless changeset fields

data  = %{}
types = %{name: :string, email: :string, age: :integer}
params = %{name: "Callum", email: "callum@example.com", age: 27}
changeset =
  {data, types}
  |> Ecto.Changeset.cast(params, Map.keys(types))
  |> Ecto.Changeset.validate_required(...)

but also want to add dynamic embedded relations

   embeds_many :response_1, Jsonb, on_replace: :delete do
      field :order, :integer
      field :text, :string
    end

   embeds_many :response_2, Jsonb, on_replace: :delete do
      field :order, :integer
      field :text, :string
    end

   embeds_many :response_3, Jsonb, on_replace: :delete do
      field :order, :integer
      field :text, :string
    end

As you noticed the embed :response_x is dynamic (it grow or shrink) and I don’t want to hardcode these embedded relations. I need find a way to just enum the responses and build these relationships at the same time be able to cast them using cast_embed.

Any idea is greatly appreciated!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement