Skip to content

Instantly share code, notes, and snippets.

@coryodaniel
Last active October 5, 2016 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryodaniel/dcf637ec14da7cbdec568ee79f03c95d to your computer and use it in GitHub Desktop.
Save coryodaniel/dcf637ec14da7cbdec568ee79f03c95d to your computer and use it in GitHub Desktop.
Elixir Ecto cast_assoc
defmodule User do
def changeset(user, params) do
user
|> cast(params, @allowed_params)
|> validate_required(@required_params)
# Account.changeset is inferred by default
|> cast_assoc(:account, required: true, with: &Account.registration_changeset/2)
end
end
account = %{plan: "free"}
user = %{email: "coolio@example.com", password: "bananas", account: account}
User.changeset(%User{}, user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment