Skip to content

Instantly share code, notes, and snippets.

@danhawkins
Last active March 20, 2022 00:05
Show Gist options
  • Save danhawkins/3538043bab48c3f363939b9ebfcd5a0c to your computer and use it in GitHub Desktop.
Save danhawkins/3538043bab48c3f363939b9ebfcd5a0c to your computer and use it in GitHub Desktop.
# priv/repo/migrations/20220319222511_create_groups.exs
defmodule TestApp.Repo.Migrations.CreateGroups do
use Ecto.Migration
def change do
create table(:groups) do
add :name, :string, null: false
add :region, :string, null: false
add :score, :integer, null: false, default: 0
end
create index(:groups, :region)
end
end
# lib/test_app/groups/group.ex
defmodule TestApp.Groups.Group do
use Ecto.Schema
import Ecto.Changeset
schema "groups" do
field(:name, :string, null: false)
field(:region, :string, null: false)
field(:score, :integer, null: false)
# Has to be virtual!
field(:group_rank, :integer, virtual: true)
end
def changeset(group, attrs) do
group
|> cast(attrs, [:name, :region, :score])
|> validate_required([:name, :region, :score])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment