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:
Hi everyone,
I have been learning a bit more on Ecto and Phoenix. Specifically in how to handle a master/detail o parent/child associations and the corresponding Web Form.
My question: Is there a better way to handle this typical scenario than the one I implemented below?
In my journey I read about:
Ecto associations, multi, cast_assoc, put_assoc, embeded_schemas, transactions
Drab Library
Checktout many resources like:
Phoenix, Ecto, Drab official documentation
Different Forums: Medium,…
This example does not apply to your actual code.
Hope this helps.
Best regards,
4 Likes
@gregvaughn , @joaquinalcerro , thank you very much. What worked was @joaquinalcerro suggestion.
1 Like