Guest User

Untitled

a guest
Jan 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. # Ecto Associations
  2.  
  3. ## Player
  4.  
  5. ```elixir
  6. schema "players" do
  7. many_to_many :games, Game, join_through: Gameplay
  8. has_many :gameplays, Gameplay
  9. # ...
  10. end
  11.  
  12. def changeset(%Player{} = player, attrs) do
  13. player
  14. |> cast_assoc(:games)
  15. |> Ecto.build_assoc(:gameplays)
  16. |> cast(...)
  17. # ...
  18. end
  19. ```
  20. ## Game
  21.  
  22. ```elixir
  23. schema "games" do
  24. many_to_many :players, Player, join_through: Gameplay
  25. has_many :gameplays, Gameplay
  26. # ...
  27. end
  28.  
  29. def changeset(%Game{} = game, attrs) do
  30. game
  31. |> cast_assoc(:players)
  32. |> Ecto.build_assoc(:gameplays)
  33. |> cast(...)
  34. # ...
  35. end
  36. ```
  37.  
  38. ## Gameplay
  39.  
  40. ```elixir
  41. schema "gameplays" do
  42. belongs_to :game, Game
  43. belongs_to :player, Player
  44. # ...
  45. end
  46.  
  47. def changeset(%Gameplay{} = gameplay, attrs) do
  48. gameplay
  49. |> cast(...)
  50. end
  51. ```
Add Comment
Please, Sign In to add comment