Xiaohong-Deng/mooqita-icccg

View on GitHub
app/models/answer.rb

Summary

Maintainability
A
0 mins
Test Coverage
class Answer < ApplicationRecord
  belongs_to :question
  belongs_to :user
  validates :question_id, uniqueness: { scope: :user_id }
  # if an entry to be updated has judge_choice = true, will check if there is already an entry
  # with the same question_id and judge_choice
  validates :question_id, uniqueness: { scope: :judge_choice }, if: Proc.new { |a| a.judge_choice }

  after_create_commit { AnswerBroadcastJob.perform_now self }

  def self.judge_choice_for(question)
    find_by(question_id: question.id, judge_choice: true)
  end

  def make_judge_choice!
    question.answers.update_all(judge_choice: false)
    update!(judge_choice: true)
  end
end