class Player < ApplicationRecord
|
|
|
|
validates_presence_of :name, message: I18n.t('please_type_your_name_to_continue')
|
|
|
|
has_many :answers, dependent: :destroy
|
|
|
|
|
|
def update_answer_cache
|
|
self.update_column(:answer_cache, self.answers.ordered.pluck(:value))
|
|
end
|
|
|
|
|
|
def stats
|
|
total_with_five = Player.where("array_length(answer_cache, 1) = 5")
|
|
.where.not(id: self.id)
|
|
.count
|
|
|
|
matching_answers = Player.where(answer_cache: self.answer_cache)
|
|
.where("array_length(answer_cache, 1) = 5")
|
|
.where.not(id: self.id)
|
|
.count
|
|
|
|
# Calculate percentage
|
|
if total_with_five > 0
|
|
percentage = (matching_answers.to_f / total_with_five) * 100
|
|
percentage_string = percentage.round.to_s
|
|
else
|
|
percentage_string = "0"
|
|
end
|
|
|
|
percentage_string
|
|
|
|
end
|
|
|
|
end
|