MatijaL

MatijaL

Help with writing Ecto.Multi function

Hello,

I’m having a bit of trouble creating a function with EctoMulti. I have support_tickets which have many support_ticket_messages. When I want to insert new message, I have the following function which takes the attrs and puts association to ticket and user and this function works just fine.

def create_support_ticket_message(%SupportTicket{} = ticket, %User{} = user, attrs) do
    %SupportTicketMessage{seen: false}
    |> SupportTicketMessage.changeset(attrs)
    |> Ecto.Changeset.put_assoc(:support_ticket, ticket)
    |> Ecto.Changeset.put_assoc(:user, user)
    |> Repo.insert()
end

Now, I want to send notification to the user when new message is created so I wanted to use Ecto.Multi() but I’m not sure how to write it properly. The following function doesn’t work because I need to use a function to use Ecto.build_assoc() and I have only a changeset which contain message attrs.

changeset = SupportTicketMessage.changeset(attrs)

Multi.new()
    |> Multi.insert(:support_ticket_message, changeset ->
        Ecto.build_assoc(ticket, :support_ticket_messages)
        Ecto.build_assoc(user, :user)
      end)
    |> Repo.transaction()

How to write this function properly?

Marked As Solved

sodapopcan

sodapopcan

Looks like you got it but since I typed this all out I’m gonna post anyway :sweat_smile: I included the run function you can use if you want notifying the user to be part of the transaction.

def support_ticket_message_changeset(%SupportTicket{} = ticket, %User{} = user, attrs) do
  %SupportTicketMessage{seen: false}
  |> SupportTicketMessage.changeset(attrs)
  |> Ecto.Changeset.put_assoc(:support_ticket, ticket)
  |> Ecto.Changeset.put_assoc(:user, user)
end

Multi.new()
|> Multi.insert(:ticket, support_ticket_changeset(ticket, user, attrs))
|> Multi.run(:notification, fn _repo, %{ticket: ticket} ->
  notify_user_about_ticket(ticket, user)
end)
|> Repo.transaction()

Also Liked

sodapopcan

sodapopcan

It’s worth noting though that if there isn’t a good reason to fail the operation if the user isn’t notified (and I would think this is generally the case) you don’t need a Multi here as a changeset with associations is automatically run in a transaction with Repo.insert and friends.

Last Post!

sodapopcan

sodapopcan

It’s worth noting though that if there isn’t a good reason to fail the operation if the user isn’t notified (and I would think this is generally the case) you don’t need a Multi here as a changeset with associations is automatically run in a transaction with Repo.insert and friends.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

We're in Beta

About us Mission Statement