When I set the run methods to return {:ok, result} it did not work because in the next line I was getting error like: could not find key record_id in :ok.record_id or something like that, but when I set the run method to return result only it worked. Like this:
def update_book_to_be_returned_multi(multi, book_id) do
alias Ecto.Multi
alias Mango.Books
alias Mango.Books.Book
Multi.run(multi, :book_to_be_returned, fn _changes ->
case Repo.get_by(Book, %{id: book_id, status: :out}) do
nil ->
{:error, %{error: "Book not found or already available.."}}
book ->
case Books.update_book(book, %{status: :available}) do
book = book -> book
nil -> {:error, %{error: "Error occured while updating the book.."}}
end
end
end)
end
And:
multi_result =
Multi.new()
|> Works.update_book_to_be_returned_multi(line.book_id)
|> Multi.run(:update_associated_record, fn %{book_to_be_returned: book_to_be_returned} ->
Records.update_record(Repo.get(Record, book_to_be_returned.record_id), %{
returned_at: :os.system_time(:seconds)
})
end)
...


















