You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't think using Ecto.Multi is possible because the foreign keys of users, useragents and repositories are required for hits ...
see: https://github.com/dwyl/hits#view-the-entity-relationship-er-diagram
But I would be very happy if someone could show us how it can be done.
You can use the result of previous operations in a Multi.
Multi.new
|> Multi.insert_or_update(:user_agent, user_agent_params)
|> Multi.insert_or_update(:user, user_params)
|> Multi.run(:repository, fn _repo, %{user: user} ->
# something that inserts/updates Repository with the user and returns {:ok, value} or {:error, value}
|> Multi.run(:hit, fn _repo, %{user_agent: user_agent, repository: repository} ->
# something that inserts/updates Hit with the user agent & repository and returns {:ok, value} or {:error, value}
At present we are using 4 queries to insert a single hit.
This works "OK" and is "fast enough" but we can probably do better ...
Thankfully @katbow wrote this helpful guide:
https://github.com/dwyl/learn-phoenix-framework/blob/master/ecto-multi.md
So it should be pretty straightforward to refactor.
Todo
insert_hit
function (and supporting functions) to use Ecto.MultiThe text was updated successfully, but these errors were encountered: