diff --git a/db/migrate/20101221143936_add_indexes_to_vote.rb b/db/migrate/20101221143936_add_indexes_to_vote.rb new file mode 100644 index 0000000..697a0ce --- /dev/null +++ b/db/migrate/20101221143936_add_indexes_to_vote.rb @@ -0,0 +1,13 @@ +class AddIndexesToVote < ActiveRecord::Migration + def self.up + add_index(:votes, :loser_choice_id, :name => 'loser_choice_id_idx') + add_index(:votes, :choice_id, :name => 'choice_id_idx') + add_index(:votes, :question_id, :name => 'question_id_idx') + end + + def self.down + remove_index(:votes, :name => :loser_choice_id_idx) + remove_index(:votes, :name => :choice_id_idx) + remove_index(:votes, :name => :question_id_idx) + end +end diff --git a/db/schema.rb b/db/schema.rb index 3062e1c..87d973a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20100727203816) do +ActiveRecord::Schema.define(:version => 20101221143936) do create_table "appearances", :force => true do |t| t.integer "voter_id" @@ -220,6 +220,9 @@ ActiveRecord::Schema.define(:version => 20100727203816) do t.string "validity_information" end + add_index "votes", ["choice_id"], :name => "choice_id_idx" + add_index "votes", ["loser_choice_id"], :name => "loser_choice_id_idx" + add_index "votes", ["question_id"], :name => "question_id_idx" add_index "votes", ["voter_id"], :name => "index_votes_on_voter_id" end diff --git a/lib/tasks/test_api.rake b/lib/tasks/test_api.rake index 125b252..f443250 100644 --- a/lib/tasks/test_api.rake +++ b/lib/tasks/test_api.rake @@ -1,5 +1,19 @@ namespace :test_api do + desc "Updates cached values for losses and wins for for choices." + task :update_cached_losses_wins => :environment do + Question.all.each do |question| + question.choices.each do |choice| + choice.reload + true_losses = question.votes.count(:conditions => {:loser_choice_id => choice.id}) + true_wins = choice.votes.count + Choice.update_counters choice.id, + :losses => (true_losses - choice.losses), + :wins => (true_wins - choice.wins) + end + end + end + task :all => [:question_vote_consistency,:generate_density_information] desc "Don't run unless you know what you are doing" -- libgit2 0.21.2