class GekanatorGame < ApplicationRecord belongs_to :user, optional: true belongs_to :guessed_post, class_name: 'Post' belongs_to :correct_post, class_name: 'Post', optional: true validates :answers, presence: true validates :question_count, numericality: { greater_than_or_equal_to: 0 } validates :won, inclusion: { in: [true, false] } validate :correct_post_required_when_lost private def correct_post_required_when_lost return if won || correct_post_id.present? errors.add(:correct_post_id, '外れた時は正解の投稿を指定してくださぃ.') end end