Anyone who had to deal with change-ing nested associations knows how strugglesome and tedious it can get.
This library provides for now three convenient functions.
- Nest a change into a changeset:
ChangesetHelpers.put_assoc(account_changeset, [:user, :config, :address], address_changeset)
A function may also be provided as third argument, which receives the nested changeset (or data wrapped by a new changeset) as argument and returns the modified changeset.
For example in the code below I change an empty entity and add it into the association (typically when you want to insert a new row of inputs in a form to add an entity into a collection of persisted entities):
ChangesetHelpers.put_assoc(account_changeset, [:user, :articles],
&(Enum.concat(&1, [%Article{} |> Ecto.Changeset.change()])))
- Change a nested association:
{account_changeset, address_changeset} =
ChangesetHelpers.change_assoc(account_changeset, [:user, :user_config, :address])
{account_changeset, address_changeset} =
ChangesetHelpers.change_assoc(account_changeset, [:user, :user_config, :address], %{street: "Foo street"})
A tuple is returned containing the modified root changeset and the association changeset.
- Check whether a given field is different between two changesets:
{street_changed, street1, street2} =
ChangesetHelpers.diff_field(account_changeset, new_account_changeset, [:user, :user_config, :address, :street])
It’s quite niche but I still wanted to share the work with you