alexcastano

alexcastano

I’m creating a complex form using LiveView. I’m using an Ecto.Changeset to render the form, so when the user changes something I update the changeset to re-render the form. I have most of the functionality working without problem, except for deleting a grandchild association.

I created a simplified example of my real code to show my case. I’m editing a Foo schema which has many Bar in :bars field. At the same time, a Bar struct has many Baz in :bazs field. The code is the following:

defmodule Project.Foo do
  use Ecto.Schema

  schema "foos" do
    field :foo, :string, default: "foo"
    has_many :bars, Project.Bar
  end

  def changeset(%__MODULE__{} = schema, params) do
    schema
    |> Ecto.Changeset.cast(params, [:foo])
    |> Ecto.Changeset.cast_assoc(:bars)
  end
end

defmodule Project.Bar do
  use Ecto.Schema

  schema "bars" do
    field :bar, :string, default: "bar"
    has_many :bazs, Project.Baz
  end

  def changeset(%__MODULE__{} = schema, params) do
    schema
    |> Ecto.Changeset.cast(params, [:bar])
    |> Ecto.Changeset.cast_assoc(:bazs)
  end
end

defmodule Project.Baz do
  use Ecto.Schema

  schema "bazs" do
    field :baz, :string, default: "baz"
  end

  def changeset(%__MODULE__{} = schema, params) do
    schema
    |> Ecto.cast(params, [:baz])
  end
end

defmodule Project.FooLive do
  use Phoenix.LiveView
  use Phoenix.HTML

  def mount(%{}, socket) do
    changeset = %Project.Foo{bars: [%Project.Bar{bazs: [%Project.Baz{}]}]} 
                |> Ecto.Changeset.change()

    {:ok, assign(socket, changeset: changeset)}
  end

  def render(assigns) do
    ~L"""
    <%= f = form_for assigns.changeset, "#", [] %>
      <%= label f, :foo %>
      <%= text_input f, :foo %>
      <%= inputs_for f, :bars, fn bar_f -> %>
        <%= label bar_f, :bar %>
        <%= text_input bar_f, :bar %>
        <%= inputs_for bar_f, :bazs, fn baz_f -> %>
          <%= label baz_f, :baz %>
          <%= text_input baz_f, :baz %>
          <button phx-click="delete_bar" phx-value="<%= "#{bar_f.index}-#{baz_f.index}" %>">Delete Bar</button>
        <% end %>
      <% end %>
    </form>
    """
  end

  def handle_event("delete_bar", indexes, socket) do
    changeset = socket.assigns.changeset
    [bar_index, baz_index] = indexes |> String.split("-") |> Enum.map(&String.to_integer/1)
    bars = Ecto.Changeset.get_field(changeset, :bars)
           |> IO.inspect(label: "pre change")
           |> List.update_at(bar_index, fn bar ->
             Map.update!(bar, :bazs, fn bazs ->
               List.delete_at(bazs, baz_index)
             end)
           end)
           # we can see here that `:bazs` == []
           |> IO.inspect(label: "post change")

    # This is false as I expected
    IO.inspect(bars == Ecto.Changeset.get_field(changeset, :bars), label: "Bars equals?")
    # I expected this to be false, but it is true because it doesn't update the bars relation
    IO.inspect(changeset == Ecto.Changeset.change(changeset, bars: bars), label: "Changeset equals?")
    # The same problem with put_assoc
    IO.inspect(changeset == Ecto.Changeset.put_assoc(changeset, :bars, bars), label: "Changeset 'put_assoc' equals?")

    changeset = Ecto.Changeset.change(changeset, bars: bars)

    {:noreply, assign(socket, changeset: changeset)}
  end
end

The output:

pre change: [
  %Project.Bar{
    __meta__: #Ecto.Schema.Metadata<:built, "bars">,
    bar: "bar",
    bazs: [
      %Project.Baz{
        __meta__: #Ecto.Schema.Metadata<:built, "bazs">,
        baz: "baz",
        id: nil
      }
    ],
    id: nil
  }
]
post change: [
  %Project.Bar{
    __meta__: #Ecto.Schema.Metadata<:built, "bars">,
    bar: "bar",
    bazs: [],
    id: nil
  }
]
Bars equals?: false
Changeset equals?: true
Changeset 'put_assoc' equals?: true

It seems like Ecto.Changeset.change does not detect that the :bars association changed. I use a similar approach to add a grandchild and it works perfectly, it detects the change and LiveView re-renders the form. I don’t know if it is a bug in Ecto or I’m not using the right function or approach—I admit the code it is quite complex just to delete a grandchild, if you have suggestion to make it easier I’m all ears :slight_smile:

Thanks in advance

Showing Posts 1 to 10

wolfiton

wolfiton

Do you use phoenix context to decouple the application?

If not the following command will create one mix phx.gen.context Project Baz baz baz

This command will generate a module named Project with the model baz and a migration baz with a field of baz string.

You can find more info here 1. Intro to Contexts — Phoenix v1.8.8

and here https://www.youtube.com/watch?v=5MBGDM8xSQg

idi527

idi527

:waving_hand:

I’d probably try to rerun the Foo.changeset/2 on foo (after manually deleting baz from it). You can get foo after every change by running apply_changes on the changeset.

  def mount(%{}, socket) do
    foo = %Project.Foo{bars: [%Project.Bar{bazs: [%Project.Baz{}]}]} 
    changeset = Foo.changeset(foo, %{})
    {:ok, assign(socket, foo: foo, changeset: changeset)}
  end

  def handle_event("deleve_bar", indexes, socket) do
    foo = delete_bar(socket.assigns.foo, indexes)
    changeset = Foo.changeset(foo, %{})
    {:noreply, assign(socket, changeset: changeset)}
  end 

  def handle_event("validate", %{"foo" => changes}, socket) do
    changeset = Foo.changeset(changeset, changes)
    foo = Ecto.Changeset.apply_changes(changeset)
    {:noreply, assign(socket, changeset: changeset, foo: foo)}
  end

  defp delete_bar(%{bars: bars} = foo, indexes) do
    [bar_index, baz_index] = indexes |> String.split("-") |> Enum.map(&String.to_integer/1)
    bars = List.update_at(bars, bar_index, fn bar ->
      Map.update!(bar, :bazs, fn bazs -> List.delete_at(bazs, baz_index) end)
    end)
    %{foo | bars: bars}
  end
alexcastano

alexcastano OP

I found a solution. It is not the same:

%Project.Foo{bars: [%Project.Bar{bazs: [%Project.Baz{}]}]} 
|> Ecto.Changeset.change()

# and

%Project.Foo{}
|> Ecto.Changeset.change(bars: [%Project.Bar{bazs: [%Project.Baz{}]}])

# or even better to build this kind of complex forms:

baz_ch = Ecto.Changeset.change(%Baz{})
bar_ch = Ecto.Changeset.change(%Bar{}, bazs: [baz_ch])
foo_ch = Ecto.Changeset.change(%Foo{}, bars: [bar_ch])

Working only with changeset it has been easier for me.

Thank you for your help @idi527 @wolfiton

idi527

idi527

It’s possible to simplify my approach to not store :foo assign

  def mount(%{}, socket) do
    foo = %Project.Foo{bars: [%Project.Bar{bazs: [%Project.Baz{}]}]} 
    changeset = Foo.changeset(foo, %{})
    {:ok, assign(socket, changeset: changeset)}
  end

  def handle_event("delete_baz", baz_id, socket) do
    foo =
      socket.assigns.changeset
      |> Ecto.Changeset.apply_changes()
      |> delete_bar(baz_id)
    changeset = Foo.changeset(foo, %{})
    {:noreply, assign(socket, changeset: changeset)}
  end

  def handle_event("validate", %{"foo" => changes}, socket) do
    changeset = Foo.changeset(changeset, changes)
    {:noreply, assign(socket, changeset: changeset)}
  end

  defp delete_baz(%{bars: bars} = foo, baz_id) do
    bars = Enum.map(bars, fn %{bazs: bazs} ->
      Enum.reject(bazs, fn baz -> baz.id == baz_id end)
    end)
    %{foo | bars: bars}
  end
alexcastano

alexcastano OP

Well, if the schema is already created in the database this approach doesn’t work, don’t you think?

idi527

idi527

By schema, do you mean the bar that we are deleting? Depending on how your schema and changeset for foo are setup, it might be deleted on Repo.update or it might not, sure, but I wouldn’t say it doesn’t work.

Note, however, the problem you are facing has nothing to do with databases, but only with changesets.

alexcastano

alexcastano OP

I mean working with a schema which is actually stored in the database, ie: if you change the Foo.foo value from “stored_value_in_db” to “new_value”, and later you delete_bar, when you apply_changes you lose the current changes. In this case, if you Repo.update(changeset) the Foo.foo value won’t be updated. I did something similar to the following to solve this problem (I didn’t try this code):

  def handle_event("delete_bar", index, socket) do
    bars = Ecto.Changeset.get_change(socket.assigns.changeset, :bars, [])
           |> List.delete_at(index)
    {:noreply, assign(socket, changeset: Ecto.Changeset.change(bars)}
  end

  def handle_event("delete_baz", indexes, socket) do
    changeset = socket.assigns.changeset
    [bar_index, baz_index] = indexes |> String.split("-") |> Enum.map(&String.to_integer/1)
    bars = Ecto.Changeset.get_change(socket.assigns.changeset, :bars, [])
           |> List.update_at(bar_index, fn bar_ch ->
       bazs = Ecto.Changeset.get_change(bar_ch, :bazs, [])
              |> List.delete_at(baz_index)
       Ecto.Changeset.change(bar_ch, bazs: bazs)
    end)
    {:noreply, assign(socket, changeset: Ecto.Changeset.change(changeset, bars: bars))}
  end

This way I avoid to apply_changes to don’t lose the changes

idi527

idi527

and later you delete_bar , when you apply_changes you lose the current changes.

No, the current changeset is passed to apply_changes/1, so we don’t lose anything.

Ah, I think I understand you now, can you show me how you handle form submit event? If you just save the changeset, then yeah, it wouldn’t include previous changes to “foo” if we’ve since handled "delete_baz" event, but I wouldn’t use that changeset when calling repo, since the underlying record might have been changed since, unless Foo.changeset uses optimistic_lock/3.

wolfiton

wolfiton

Glad i could provide some help

alexcastano

alexcastano OP

Yeah, that’s my point, then I just Repo.update(changeset).

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews