Last active
October 16, 2020 19:42
-
-
Save marinho10/af1f1af0f7a3632c2014fa237fe51343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]))