Skip to content

Instantly share code, notes, and snippets.

@Donavan
Created November 29, 2016 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Donavan/e32f1d1f96d44771f7578306e7ee10e6 to your computer and use it in GitHub Desktop.
Save Donavan/e32f1d1f96d44771f7578306e7ee10e6 to your computer and use it in GitHub Desktop.
defmodule Web.Team do
use Web.Web, :model
schema "teams" do
field :name, :string
many_to_many :users, join_through: 'team_members', on_delete: :delete_all
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name])
|> validate_required([:name])
end
end
defmodule Web.TeamMember do
use Web.Web, :model
schema "team_members" do
has_one :user, User
has_one :team, Team
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:user_id, :team_id])
|> validate_required([:user_id, :team_id])
end
end
defmodule Web.User do
use Web.Web, :model
use Coherence.Schema
schema "users" do
field :name, :string
field :email, :string
coherence_schema
many_to_many :teams, Web.Team, join_through: 'team_members'
timestamps
end
def changeset(model, params \\ %{}) do
model
|> cast(params, [:name, :email] ++ coherence_fields)
|> validate_required([:name, :email])
|> unique_constraint(:email)
|> validate_coherence(params)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment