diff --git a/app/models/choice.rb b/app/models/choice.rb index 5c73aea..022ff77 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -16,6 +16,11 @@ class Choice < ActiveRecord::Base item.data end + def lose! + self.loss_count += 1 + save! + end + def wins_plus_losses #(prompts_on_the_left.collect(&:votes_count).sum + prompts_on_the_right.collect(&:votes_count).sum) Prompt.sum('votes_count', :conditions => "left_choice_id = #{id} OR right_choice_id = #{id}") diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 87ec363..fabc6fa 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -13,12 +13,16 @@ class Visitor < ActiveRecord::Base end def vote_for!(prompt, ordinality) - choice = prompt.choices[ordinality] #we need to guarantee that the choices are in the right order (by position) + choices = prompt.choices + choice = choices[ordinality] #we need to guarantee that the choices are in the right order (by position) prompt_vote = votes.create!(:voteable => prompt) choice_vote = votes.create!(:voteable => choice) choice.save! choice.score = choice.compute_score choice.save! + + other_choices = choices - [choice] + other_choices.each {|c| c.lose! } end def skip!(prompt) -- libgit2 0.21.2