Skip to content

Instantly share code, notes, and snippets.

@cblavier
Created May 3, 2021 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cblavier/356e662fdbc7910cc4bc39737d7851c2 to your computer and use it in GitHub Desktop.
Save cblavier/356e662fdbc7910cc4bc39737d7851c2 to your computer and use it in GitHub Desktop.
Ecto put_assoc with an array of ids
tags = for id <- [40, 41, 42] do
%Tag{id: id, __meta__: %Ecto.Schema.Metadata{source: "tags", state: :loaded}}
end
Book
|> Repo.get(1000)
|> Repo.preload(:tags)
|> Book.changeset()
|> Ecto.Changeset.put_assoc(:tags, tags)
|> Repo.update()
@user20230119
Copy link

Why does this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment