Created
January 31, 2019 11:02
-
-
Save marinho10/f1a86b01529d5344ecb7dc908c40313b to your computer and use it in GitHub Desktop.
This file contains 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
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