jan-swiatek
Ecto.Multi error handling
Hi
,
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
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 ![]()
1
Popular in Questions
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
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
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









