Solution: Work with Transactions and the Multi Module
Go over the solution to the "Work with Transactions and the Multi Module" problem.
Solution
We are going to rewrite this code by using Ecto.Multi
.
cs = Ecto.Changeset.change(%Album{title: "Bag's Groove", artist_id: 1})|> Ecto.Changeset.validate_required([:title])Repo.transaction(fn ->case Repo.insert(cs) do{:ok, _album} -> IO.puts("Album insert succeeded"){:error, _value} -> Repo.rollback("Album insert failed")endcase Repo.insert(Log.changeset_for_insert(cs)) do{:ok, _log} -> IO.puts("Log insert succeeded"){:error, _value} -> Repo.rollback("Log insert failed")endend)
It inserts a new album record as well as a log entry. We used an anonymous function with the ...