Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helpers for updating nested changesets #3731

Closed
wants to merge 2 commits into from
Closed

helpers for updating nested changesets #3731

wants to merge 2 commits into from

Conversation

woylie
Copy link
Contributor

@woylie woylie commented Aug 27, 2021

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 to Ecto.Changeset.

  • append_at/3: appends a new item to an array field or has_many association at the given path
  • insert_at/3: inserts a new item at a path/position
  • update_at/3: updates a change or field at the given path
  • delete_at/2: deletes an item at a given path/position

The 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 at root.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.

@josevalim
Copy link
Member

Thanks @woylie! I agree with it in principle but I am not sure how happy Ecto is going to be about reading the value and putting the same value back. So may I suggest you create a ecto_nested_changeset package to battle test those ideas out? I appreciate the ping!

@woylie
Copy link
Contributor Author

woylie commented Aug 27, 2021

Thanks @josevalim! I moved the code to https://github.com/woylie/ecto_nested_changeset. I'll let you know once I have a working version.

@josevalim
Copy link
Member

Beautiful! Closing for this now!

@josevalim josevalim closed this Aug 27, 2021
@woylie woylie deleted the nested-changeset-helpers branch August 27, 2021 07:05
@woylie
Copy link
Contributor Author

woylie commented Aug 28, 2021

Hi @josevalim, just pinging you to let you know that a first version has been released: https://github.com/woylie/ecto_nested_changeset

There is an example Phoenix application in the /example folder.

@josevalim
Copy link
Member

Sweet, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants