Skip to content

Instantly share code, notes, and snippets.

@Copser
Created June 14, 2019 09:38
Show Gist options
  • Save Copser/254972bc533327d7a6c9ad94b9a8547a to your computer and use it in GitHub Desktop.
Save Copser/254972bc533327d7a6c9ad94b9a8547a to your computer and use it in GitHub Desktop.
many to many associations ecto
user = Repo.get!(User, user_id) |> Repo.preload(:roles)
role = Repo.get!(Role, role_id)
user_changeset = Changeset.change(user) |> Changeset.put_assoc(:roles, [role | user.roles])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment