Skip to content

Instantly share code, notes, and snippets.

@marinho10
Last active October 16, 2020 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinho10/af1f1af0f7a3632c2014fa237fe51343 to your computer and use it in GitHub Desktop.
Save marinho10/af1f1af0f7a3632c2014fa237fe51343 to your computer and use it in GitHub Desktop.
def MyApp.Accounts.User do
...
def changeset_update_projects(%User{} = user, projects) do
user
|> cast(%{}, @required_fields)
# associate projects to the user
|> put_assoc(:projects, projects)
end
...
end
@karlosmid
Copy link

Hi!
By testing my implementation of many to many relations, I found an issue on this line:

https://gist.github.com/marinho10/af1f1af0f7a3632c2014fa237fe51343#file-changeset_update_projects-ex-L7

When you want to add many to many relations, list concatenation should be done in order not to lose existing relations:

|> put_assoc(:projects, List.flatten([projects | user.projects]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment