jan-swiatek

jan-swiatek

Ecto.Multi error handling

Hi :waving_hand:,

I want to insert user and company at once (user belongs to company, company can have many users) using Ecto.Multi. Here is my current form:

  <.simple_form :let={f} for={@changeset} action={~p"/users/register"}>
    <.inputs_for :let={company} field={f[:company]}>
      <.input field={company[:name]} type="text" label="Company" />
    </.inputs_for>

    <.input field={f[:email]} type="email" label="Email" required />
    <.input field={f[:password]} type="password" label="Password" required />

    <:actions>
      <.button phx-disable-with="Creating account..." class="w-full">Create an account</.button>
    </:actions>
  </.simple_form>

and Ecto.Multi transaction:

  def register(user_params, company_params) do
    Multi.new()
    |> Multi.run(:company, fn _repo, _changes ->
      case Companies.create_company(company_params) do
        {:ok, %{company: company}} -> {:ok, company}
        {:error, :company, changeset, _} -> {:error, changeset}
      end
    end)
    |> Multi.run(:user, fn _repo, %{company: company} ->
      user_params
      |> Map.put("company_id", company.id)
      |> Accounts.register_user()
    end)
    |> Repo.transaction()
  end

However if something went wrong while creating company (e.g. name was already taken) I got Company changeset, what causes an error: “could not generate inputs for :company from MyApp.Companies” (it expects User changeset). What is the most idiomatic approach in this case? I can’t use cast_assoc, since create_company is Multi transaction itself (it also invokes some additional steps).

Most Liked

dimitarvp

dimitarvp

It’s useful for future readers if you show how.

jan-swiatek

jan-swiatek

Thanks for replies. I ended up with reorganising code a bit (I changed order of functions in transactions) and I was able to use cast_assoc :slight_smile:

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
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement