Commit 6d3412e9c59a8ca08f26aba01d761b843ba9ed47

Authored by Luke Baker
1 parent b30ff5ab

fix catchup prompt bug and increase speed

Showing 1 changed file with 9 additions and 9 deletions   Show diff stats
app/models/question.rb
... ... @@ -115,20 +115,20 @@ class Question < ActiveRecord::Base
115 115 sum = 0.0
116 116  
117 117 # get weights of all existing prompts that have two active choices
118   - active_choice_ids = choices.active.map {|c| c.id}
119   - prompts.find_each(:select => 'votes_count, left_choice_id, right_choice_id',
120   - :conditions => {:left_choice_id => active_choice_ids,
121   - :right_choice_id => active_choice_ids}) do |p|
122   - value = [(1.0/ (p.votes.size + 1).to_f).to_f, throttle_min].min
123   - weights["#{p.left_choice_id}, #{p.right_choice_id}"] = value
  118 + active_choices = choices.active
  119 + active_choice_ids = active_choices.map {|c| c.id}
  120 + sql = "SELECT votes_count, left_choice_id, right_choice_id FROM prompts WHERE question_id = #{self.id} AND left_choice_id IN (#{active_choice_ids.join(',')}) AND right_choice_id IN (#{active_choice_ids.join(',')})"
  121 + ActiveRecord::Base.connection.select_all(sql).each do |p|
  122 + value = [(1.0/ (p['votes_count'].to_i + 1).to_f).to_f, throttle_min].min
  123 + weights[p['left_choice_id']+", "+p['right_choice_id']] = value
124 124 sum += value
125 125 end
126 126  
127 127 # This will not run once all prompts have been generated,
128 128 # but it prevents us from having to pregenerate all possible prompts
129   - if weights.size < choices.active.size ** 2 - choices.active.size
130   - choices.active.each do |l|
131   - choices.active.each do |r|
  129 + if weights.size < active_choices.size ** 2 - active_choices.size
  130 + active_choices.each do |l|
  131 + active_choices.each do |r|
132 132 if l.id == r.id
133 133 next
134 134 end
... ...