Skip to content

Instantly share code, notes, and snippets.

@marinho10
Last active January 31, 2019 16:06
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 marinho10/a4bafad17eead249b777bb108373817f to your computer and use it in GitHub Desktop.
Save marinho10/a4bafad17eead249b777bb108373817f to your computer and use it in GitHub Desktop.
defmodule MyApp.Management.UserProject do
@moduledoc """
UserProject module
"""
use Ecto.Schema
import Ecto.Changeset
alias MyApp.Accounts.User
alias MyApp.Management.Project
@already_exists "ALREADY_EXISTS"
@primary_key false
schema "user_project" do
belongs_to(:user, User, primary_key: true)
belongs_to(:project, Project, primary_key: true)
timestamps()
end
@required_fields ~w(user_id project_id)a
def changeset(user_project, params \\ %{}) do
user_project
|> cast(params, @required_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:project_id)
|> foreign_key_constraint(:user_id)
|> unique_constraint([:user, :project],
name: :user_id_project_id_unique_index,
message: @already_exists
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment