Last active
February 19, 2017 04:08
-
-
Save pedantix/8c81e528cd14021f1db6d817a7dd0f25 to your computer and use it in GitHub Desktop.
Using cast/3 with @optional_fields and @required_fields for simplicity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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