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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement