Skip to content

Instantly share code, notes, and snippets.

@dthomas
Created November 16, 2015 04:47
Show Gist options
  • Select an option

  • Save dthomas/e7341daf3dfec7bc1a3a to your computer and use it in GitHub Desktop.

Select an option

Save dthomas/e7341daf3dfec7bc1a3a to your computer and use it in GitHub Desktop.
Elixir Phoenix
def create(changeset, repo) do
password = Ecto.Changeset.get_field(changeset, :password)
team_name = Ecto.Changeset.get_field(changeset, :name)
changeset = changeset
|> Ecto.Changeset.put_change(:password_digest, crypt_password(password))
repo.transaction(fn ->
case repo.insert(changeset) do
{:ok, user} ->
team = %Team{name: team_name, owner_id: user.id}
case repo.insert(team) do
{:ok, team} ->
user_team = %UserTeam{team_id: team.id, user_id: user.id, role: "owner"}
case repo.insert(user_team) do
{:ok, user_team} ->
{:ok, user_team}
end
{:error, changeset} ->
{:error, changeset}
end
{:error, changeset} ->
{:error, changeset}
end
end)
end
@dthomas

dthomas commented Nov 16, 2015

Copy link
Copy Markdown
Author

I am getting an error when a uniqueness constraint check fails

Server: localhost:4000 (http)
Request: POST /registrations
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Phoenix.HTML.FormData not implemented for :rollback
(phoenix_html) lib/phoenix_html/form_data.ex:1: Phoenix.HTML.FormData.impl_for!/1
(phoenix_html) lib/phoenix_html/form_data.ex:15: Phoenix.HTML.FormData.to_form

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