I have an Ecto schema setup that has an has_many
association. I want to be able to dynamically add/remove associations to it, keeping the initial association.
I've tried to use Ecto.Changeset.put_assoc/4
and that works when loading the initial association, however each subsequent call will override the initial association.
change_one = Changeset.put_assoc(changeset, :foo_assocs, [%{index: 1}])
...
foo_assocs: [
#Ecto.Changeset<
action: :insert,
changes: %{index: 1},
errors: [],
data: #Linker.CustomForms.FooAssoc<>,
valid?: true
>
]
...
Then if I call it again with another associated record to add:
change_two = Changeset.put_assoc(changeset_one, :foo_assocs, [%{index: 2}])
...
foo_assocs: [
#Ecto.Changeset<
action: :insert,
changes: %{index: 2},
errors: [],
data: #Linker.CustomForms.FooAssoc<>,
valid?: true
>
]
...
My first record is overwritten.