32 lines
794 B
Crystal
32 lines
794 B
Crystal
require "granite"
|
|
|
|
module MW
|
|
module Db
|
|
class TeacherVote < Granite::Base
|
|
table teacher_votes
|
|
|
|
belongs_to :vote
|
|
belongs_to :teacher
|
|
|
|
column id : Int64, primary: true
|
|
column priority : Int32
|
|
|
|
validate :teacher, "must be present" do |teacher_vote|
|
|
!teacher_vote.teacher.nil?
|
|
end
|
|
|
|
validate :teacher, "must be student unique" do |teacher_vote|
|
|
self.where(vote_id: teacher_vote.vote.id, teacher_id: teacher_vote.teacher.not_nil!.id).count == 0
|
|
end
|
|
|
|
validate :priority, "must be greater than 0" do |teacher_vote|
|
|
teacher_vote.priority > 0
|
|
end
|
|
|
|
validate :priority, "must be less than the number of teachers" do |teacher_vote|
|
|
teacher_vote.priority < Teacher.count
|
|
end
|
|
end
|
|
end
|
|
end
|