helpers for updating nested changesets #3731
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proposal for the addition of some helper functions for manipulating nested changesets.
One common use case for changesets is as
FormData
for forms in Phoenix. With LiveView, we can easily build complex, dynamic, nested forms, but Ecto doesn't expose functionality for easily manipulating nested associations at the moment. I'm proposing the addition of four functions toEcto.Changeset
.append_at/3
: appends a new item to an array field orhas_many
association at the given pathinsert_at/3
: inserts a new item at a path/positionupdate_at/3
: updates a change or field at the given pathdelete_at/2
: deletes an item at a given path/positionThe path argument in all these functions would be a list of atoms (to reference fields) and integers (to reference the index in a list). For example,
append_at(changeset, [:posts, 4, :comments], %Comment{})
would append a comment atroot.posts[4].comments
.delete_at(changeset, [:posts, 3])
would delete the post with index 3.I sketched out a rough implementation for
append_at/3
in this PR. Before I flesh this out to handle all cases and errors, I'd like to hear someone's thoughts on this effort.