30 lines
809 B
Elixir
30 lines
809 B
Elixir
|
|
defmodule Rsh26pool.Voting.Vote do
|
||
|
|
@moduledoc """
|
||
|
|
A single vote cast by a participant for an idea. A participant may vote for a
|
||
|
|
given idea at most once (enforced by a unique index).
|
||
|
|
"""
|
||
|
|
use Ecto.Schema
|
||
|
|
import Ecto.Changeset
|
||
|
|
|
||
|
|
alias Rsh26pool.Voting.{Idea, Participant}
|
||
|
|
|
||
|
|
schema "votes" do
|
||
|
|
belongs_to :idea, Idea
|
||
|
|
belongs_to :participant, Participant
|
||
|
|
|
||
|
|
timestamps(type: :utc_datetime)
|
||
|
|
end
|
||
|
|
|
||
|
|
def changeset(vote, attrs) do
|
||
|
|
vote
|
||
|
|
|> cast(attrs, [:idea_id, :participant_id])
|
||
|
|
|> validate_required([:idea_id, :participant_id])
|
||
|
|
|> unique_constraint([:idea_id, :participant_id],
|
||
|
|
name: :votes_idea_id_participant_id_index,
|
||
|
|
message: "は既に投票済みです"
|
||
|
|
)
|
||
|
|
|> foreign_key_constraint(:idea_id)
|
||
|
|
|> foreign_key_constraint(:participant_id)
|
||
|
|
end
|
||
|
|
end
|