How to return a specific object (not last one) in a Ecto.Multi chain

I have this:

|> Multi.insert(:record, record_changeset)
|> Ecto.Multi.run(:update_book, fn %{record: record} ->
  Books.update_book(Books.get_book!(book.book_id), %{status: 0, record_id: record.id})
end)
|> Repo.transaction()

This returns the book object but I want it to return the record object (from previous step), how to do that? Thank you.

I haven’t done this, but you can give an optional 2nd param to Multi.insert with a returning: true keyword which should cause the inserted Ecto struct to be returned. It should be part of the return value of the Multi under the :record key

1 Like

So,

When you run Repo.transaction(), your Multi runs. This will return a map like this:

%{:ok, %{record: record, update_book: result}}

You can use this tuple un an case. For example check this post:

This example does not apply to your actual code.

Hope this helps.

Best regards,

3 Likes

@gregvaughn, @joaquinalcerro, thank you very much. What worked was @joaquinalcerro suggestion.

1 Like