diff --git a/lib/chess/auth/user.ex b/lib/chess/auth/user.ex index 5090d85..2909073 100644 --- a/lib/chess/auth/user.ex +++ b/lib/chess/auth/user.ex @@ -11,6 +11,7 @@ defmodule Chess.Auth.User do field :username, :string has_many :games, Chess.Store.Game + has_many :other_games, Chess.Store.Game, foreign_key: :opponent_id timestamps() end diff --git a/lib/chess/store/game.ex b/lib/chess/store/game.ex index 6db4ba5..339e762 100644 --- a/lib/chess/store/game.ex +++ b/lib/chess/store/game.ex @@ -13,6 +13,7 @@ defmodule Chess.Store.Game do field :board, :map belongs_to :user, Chess.Auth.User + belongs_to :opponent, Chess.Auth.User, references: :id timestamps() end diff --git a/priv/repo/migrations/20180218165621_add_opponent_to_game.exs b/priv/repo/migrations/20180218165621_add_opponent_to_game.exs new file mode 100644 index 0000000..3fefa2c --- /dev/null +++ b/priv/repo/migrations/20180218165621_add_opponent_to_game.exs @@ -0,0 +1,9 @@ +defmodule Chess.Repo.Migrations.AddOpponentToGame do + use Ecto.Migration + + def change do + alter table("games") do + add :opponent_id, references(:users) + end + end +end