Skip to content

Instantly share code, notes, and snippets.

@abitdodgy
Created November 21, 2016 19:31
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 abitdodgy/d6980d9ac65cd7a61b3d4d74c6d728a4 to your computer and use it in GitHub Desktop.
Save abitdodgy/d6980d9ac65cd7a61b3d4d74c6d728a4 to your computer and use it in GitHub Desktop.
Medium Article: Medium Article: Building Many-to-Many Associations with cast_assoc and Nested Forms in Phoenix and Ecto
defmodule App.Membership do
# ...
def changeset(struct, params \\ %{}) do
struct
# ...
|> put_role
|> cast_assoc(:user, required: true)
end
defp put_role(changeset) do
case changeset do
%Ecto.Changeset{valid?: true} ->
put_change(changeset, :role, :admin)
_ ->
changeset
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment