Created
March 18, 2022 22:50
-
-
Save mreigen/7a4aea23915ab1d06a1b24cf73ce4810 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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