Skip to content

Instantly share code, notes, and snippets.

@cblavier

cblavier/put_assoc_id.ex Secret

Created May 3, 2021
Embed
What would you like to do?
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment