Skip to content

Instantly share code, notes, and snippets.

@pedantix
Last active February 19, 2017 04:08
Show Gist options
  • Save pedantix/8c81e528cd14021f1db6d817a7dd0f25 to your computer and use it in GitHub Desktop.
Save pedantix/8c81e528cd14021f1db6d817a7dd0f25 to your computer and use it in GitHub Desktop.
Using cast/3 with @optional_fields and @required_fields for simplicity
defmodule Splife.Bar do
use Splife.Web, :model
schema "bars" do
field :baz, :string
field :fizz, :string
timestamps()
end
@required_fields ~w(baz)a
@optional_fields ~w(fizz)a
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment