thojanssens1

thojanssens1

Receiving a list of IDs in params for a has_many association

Say user input params (from a form, GraphQL document, etc.) look like below (let’s say a User has many Articles):

%{
  "name" => "John",
  "articles" => [5, 9, 18]
} 

or I can change it to below if that’s better:

%{
  "name" => "John",
  "articles" => [%{id: 5}, %{id: 9}, %{id: 18}]
} 

The user has selected a list of articles through a multi-select component.

How should I insert/update the association articles for the User?

Most Liked

Adzz

Adzz

There are a few ways.

  1. Use Nested Changesets
  2. Insert each item individually in a transaction. (You would need to insert the user first so you have it’s id, then each article).
  3. Use an EctoMulti.

The mutli is the same idea as 2 just a different way to do it.

If the user exists already you can upsert the user instead.

For 1 you can either do something like this:

Ecto.Changeset.cast(%User{}, changes, [:name])
|> Ecto.Changeset.cast_assoc(:articles)

Read more about cast assoc here https://link.medium.com/ZVBaeIrqM8

And check out the docs here https://hexdocs.pm/ecto/Ecto.Changeset.html

thojanssens1

thojanssens1

I want to use changeset; I don’t really see a reason to go for the 2. or 3. suggestion here.

Giving a list of IDs won’t work for cast_assoc (e.g. [5, 9, 18]). This is what is sent from Absinthe.

I’m not sure how to best solve this problem.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement