Last active
March 20, 2022 00:05
-
-
Save danhawkins/3538043bab48c3f363939b9ebfcd5a0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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