From 5aa61336eece6a07d8ca9b06b8d32a68a5e662c6 Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Wed, 22 Jun 2011 17:04:21 -0400 Subject: [PATCH] add relationships to choice to reduce memory usage of api test --- app/models/choice.rb | 6 ++++++ lib/tasks/test_api.rake | 7 ++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/models/choice.rb b/app/models/choice.rb index 17a52d6..b738114 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -12,6 +12,12 @@ class Choice < ActiveRecord::Base has_many :flags has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" + + + has_many :appearances_on_the_left, :through => :prompts_on_the_left, :source => :appearances + has_many :appearances_on_the_right, :through => :prompts_on_the_right, :source => :appearances + has_many :skips_on_the_left, :through => :prompts_on_the_left, :source => :skips + has_many :skips_on_the_right, :through => :prompts_on_the_right, :source => :skips named_scope :active, :conditions => { :active => true } named_scope :inactive, :conditions => { :active => false} diff --git a/lib/tasks/test_api.rake b/lib/tasks/test_api.rake index 013899a..18e88b7 100644 --- a/lib/tasks/test_api.rake +++ b/lib/tasks/test_api.rake @@ -64,11 +64,8 @@ namespace :test_api do def verify_choice_appearances_and_votes(choice) success_message = "Choice has more appearances than votes and skips" - prompts_on_left = choice.prompts_on_the_left.map { |p| p.id } - prompts_on_right = choice.prompts_on_the_right.map { |p| p.id } - all_prompt_ids = prompts_on_left + prompts_on_right - all_appearances = Appearance.count(:conditions => { :prompt_id => all_prompt_ids}) - skips = Skip.count(:conditions => {:prompt_id => all_prompt_ids}) + all_appearances = choice.appearances_on_the_left.count + choice.appearances_on_the_right.count + skips = choice.skips_on_the_left.count + choice.skips_on_the_right.count if all_appearances < choice.wins + choice.losses + skips error_message = "Choice #{choice.id} in Question ##{choice.question_id} has fewer appearances than wins + losses + skips" -- libgit2 0.21.2