Skip to content

Instantly share code, notes, and snippets.

@mreigen
Created March 18, 2022 22:50
Show Gist options
  • Save mreigen/7a4aea23915ab1d06a1b24cf73ce4810 to your computer and use it in GitHub Desktop.
Save mreigen/7a4aea23915ab1d06a1b24cf73ce4810 to your computer and use it in GitHub Desktop.
children_changes_params = [
%{"id" => 1, "age" => 30, "address" => "Los Angeles"},
%{"id" => 2, "age" => 28, "address" => "Manila"},
%{"id" => 3, "age" => 37, "address" => "Toulouse"}
]
multi =
Enum.reduce(children_changes_params, Ecto.Multi.new(), fn %{"id" => id} = child_params,
multi ->
with {:ok, child} <- Repo.find(Child, id) do
cs = cast(child, child_params, [:age, :address])
Ecto.Multi.update(multi, "update_#{id}", cs)
end
end)
case Repo.transaction(multi) do
{:ok, _} ->
{:ok, :updated}
{:error, _, error, _} ->
{:error, error}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment