Wigny

Wigny

Hey folks,

I have a problem that does not seem to have a straightforward solution to me, but it feels like it should have one:
How should we go about saving a form (adding/removing/updating entries) that produces a list of changesets, makes the operations in the DB and then refreshes the form with the new values?

I know that is possible when the form is based on a schema that has an embeds_many or has_many, as we have a guide in LiveView docs and also the excellent One-to-Many LiveView Form | Benjamin Milde article from @lostkobrakai, but given I would like to change multiples entries that doesn’t belongs_to a unique schema, how can I put this together inside a single form?

This question comes from a page that I’m trying to build that should allow the user to add a new entry in the DB, update an existing one or delete it, like in a bulk operation. Also, the table should have constraints, and therefore I thought using changesets would make it easy to handle and return error messages for inputs.

I’ve put a gist that goes into the direction of what I want, but I’m stuck in saving the list of changesets and refreshing the form based on this list:

How would you tackle that? Would you simply not use form to solve this problem?

Showing Posts 1 to 4

LostKobrakai

LostKobrakai

Why do you think you cannot create one? It could even live just in the controller/live view rendering your form.

Wigny

Wigny OP

I believe I’m trying this approach with the MailingList embedded schema here. The thing is that I want to insert/update/delete only the changeset list of emails. Given I can grab it with get_embed(mailing_list, :emails), I tried iterating over the list of emails and applying them to the DB, but I could not figure out how to apply them back in the MailingList changeset.

Is having a MailingList embedded schema what you had in mind?

codeanpeace

codeanpeace

Have you tried put_embed/4 from Ecto.Changeset — Ecto v3.14.0?

Wigny

Wigny OP

Yes, I have. I think the form wasn’t behaving correctly when I tried it because I was calling put_embed after the changesets being applied to the DB, which caused them to have the action replace and it left the form in a wrong state.

I guess I found a workaround for that, which is to reinitialise the original changeset instead of trying to update it after applying the operations to the DB. The code is something like this now:

def save(changeset) do
  emails_changesets = get_embed(changeset, :emails)

  case apply_changesets(emails_changesets) do
    {:ok, emails_list} -> {:ok, MailingList.changeset(%MailingList{emails: emails_list})}
    {:error, emails_changesets} -> {:error, put_embed(changeset, :emails, emails_changesets)}
  end
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews