From 3efd083fa1723726adb2aa5304fec60ec70f9126 Mon Sep 17 00:00:00 2001 From: Pius Uzamere Date: Fri, 4 Dec 2009 09:43:33 -0500 Subject: [PATCH] cast question votes so that we can cache the total vote count --- app/controllers/questions_controller.rb | 3 +-- app/models/question.rb | 5 ----- app/models/visitor.rb | 6 ++++++ db/migrate/20091204144003_update_vote_count_for_questions.rb | 11 +++++++++++ 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20091204144003_update_vote_count_for_questions.rb diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index b421441..7c41a49 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -7,8 +7,7 @@ class QuestionsController < InheritedResources::Base show! do |format| session['prompts_ids'] ||= [] format.xml { - #render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id, :votes_count, :creator_id]) - render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id, :votes_count]) + render :xml => @question.to_xml(:methods => [:item_count, :left_choice_text, :right_choice_text, :picked_prompt_id]) } end end diff --git a/app/models/question.rb b/app/models/question.rb index d457b70..43074b7 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -48,11 +48,6 @@ class Question < ActiveRecord::Base u.questions_voted_on.include? self end - def votes_count - Vote.count(:all, :conditions => {:voteable_id => id, :voteable_type => 'Question'}) - end - - validates_presence_of :site, :on => :create, :message => "can't be blank" validates_presence_of :creator, :on => :create, :message => "can't be blank" diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 5bf870d..66932bd 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -5,6 +5,7 @@ class Visitor < ActiveRecord::Base has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id" has_many :items, :class_name => "Item", :foreign_key => "creator_id" has_many :clicks + validates_presence_of :site, :on => :create, :message => "can't be blank" validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id @@ -13,10 +14,15 @@ class Visitor < ActiveRecord::Base end def vote_for!(prompt, ordinality) + question_vote = votes.create!(:voteable => prompt.question) + logger.info "Visitor: #{self.inspect} voted for Question: #{prompt.question.inspect}" + + 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) logger.info "Visitor: #{self.inspect} voted for Prompt: #{prompt.inspect}" + choice_vote = votes.create!(:voteable => choice) logger.info "Visitor: #{self.inspect} voted for Choice: #{choice.inspect}" choice.save! diff --git a/db/migrate/20091204144003_update_vote_count_for_questions.rb b/db/migrate/20091204144003_update_vote_count_for_questions.rb new file mode 100644 index 0000000..4ec8fc4 --- /dev/null +++ b/db/migrate/20091204144003_update_vote_count_for_questions.rb @@ -0,0 +1,11 @@ +class UpdateVoteCountForQuestions < ActiveRecord::Migration + def self.up + Question.reset_column_information + Question.find(:all).each do |q| + Question.update_counters q.id, :votes_count => q.choices.collect(&:votes_count).sum + end + end + + def self.down + end +end -- libgit2 0.21.2