Commit 5aa61336eece6a07d8ca9b06b8d32a68a5e662c6
1 parent
be0b6760
Exists in
master
and in
1 other branch
add relationships to choice to reduce memory usage of api test
Showing
2 changed files
with
8 additions
and
5 deletions
Show diff stats
app/models/choice.rb
| ... | ... | @@ -12,6 +12,12 @@ class Choice < ActiveRecord::Base |
| 12 | 12 | has_many :flags |
| 13 | 13 | has_many :prompts_on_the_left, :class_name => "Prompt", :foreign_key => "left_choice_id" |
| 14 | 14 | has_many :prompts_on_the_right, :class_name => "Prompt", :foreign_key => "right_choice_id" |
| 15 | + | |
| 16 | + | |
| 17 | + has_many :appearances_on_the_left, :through => :prompts_on_the_left, :source => :appearances | |
| 18 | + has_many :appearances_on_the_right, :through => :prompts_on_the_right, :source => :appearances | |
| 19 | + has_many :skips_on_the_left, :through => :prompts_on_the_left, :source => :skips | |
| 20 | + has_many :skips_on_the_right, :through => :prompts_on_the_right, :source => :skips | |
| 15 | 21 | named_scope :active, :conditions => { :active => true } |
| 16 | 22 | named_scope :inactive, :conditions => { :active => false} |
| 17 | 23 | ... | ... |
lib/tasks/test_api.rake
| ... | ... | @@ -64,11 +64,8 @@ namespace :test_api do |
| 64 | 64 | |
| 65 | 65 | def verify_choice_appearances_and_votes(choice) |
| 66 | 66 | success_message = "Choice has more appearances than votes and skips" |
| 67 | - prompts_on_left = choice.prompts_on_the_left.map { |p| p.id } | |
| 68 | - prompts_on_right = choice.prompts_on_the_right.map { |p| p.id } | |
| 69 | - all_prompt_ids = prompts_on_left + prompts_on_right | |
| 70 | - all_appearances = Appearance.count(:conditions => { :prompt_id => all_prompt_ids}) | |
| 71 | - skips = Skip.count(:conditions => {:prompt_id => all_prompt_ids}) | |
| 67 | + all_appearances = choice.appearances_on_the_left.count + choice.appearances_on_the_right.count | |
| 68 | + skips = choice.skips_on_the_left.count + choice.skips_on_the_right.count | |
| 72 | 69 | |
| 73 | 70 | if all_appearances < choice.wins + choice.losses + skips |
| 74 | 71 | error_message = "Choice #{choice.id} in Question ##{choice.question_id} has fewer appearances than wins + losses + skips" | ... | ... |