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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement