Skip to content

Instantly share code, notes, and snippets.

@terakilobyte
Created January 8, 2016 13:52
Show Gist options
  • Save terakilobyte/3a8f686698256d36c9f8 to your computer and use it in GitHub Desktop.
Save terakilobyte/3a8f686698256d36c9f8 to your computer and use it in GitHub Desktop.
defmodule Votio.Topic do
use Votio.Web, :model
schema "topics" do
field :title, :string
field :categories, :map
field :voted_by, {:array, :id}
timestamps
end
@required_fields ~w(title categories)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> unique_constraint(:title)
|> validate_length(:categories, min: 2)
|> validate_length(:topic, min: 5)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment