Commit 06017fb765b1fb6ca0c959c343281f7534818fe4

Authored by Pius Uzamere
1 parent d66be11a

fixed error with new prompt picking that, strangely, only shows up in production

Showing 1 changed file with 22 additions and 5 deletions   Show diff stats
app/models/question.rb
... ... @@ -20,12 +20,29 @@ class Question < ActiveRecord::Base
20 20 def item_count
21 21 choices_count
22 22 end
23   -
24   - def picked_prompt
  23 +
  24 + #TODO: generalize for prompts of rank > 2
  25 + #TODO: add index for rapid finding
  26 + def picked_prompt(rank = 2)
  27 + logger.info "inside picked_prompt with rank #{rank}"
  28 + raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2
  29 + logger.info "didn't raise"
  30 + choice_id_array = distinct_array_of_choice_ids(rank)
  31 + logger.info "choice id array is #{choice_id_array}"
  32 + logger.info "about to do the dynamic find_or_create ..."
  33 + @p = prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1])
  34 + end
  35 +
  36 + def distinct_array_of_choice_ids(rank = 2, only_active = true)
  37 + logger.info "inside #distinct_array_of_choice_ids"
  38 + logger.info "there are #{choices.active.count} available choices"
25 39 begin
26   - @p = prompts.first(:order => 'RANDOM()', :include => [{ :left_choice => :item }, { :right_choice => :item }])
27   - end until @p.active?
28   - return @p
  40 + @the_choice_ids = Set.new
  41 + @the_choice_ids << choices.active.first(:order => 'RANDOM()', :select => 'id').id
  42 + @the_choice_ids << choices.active.last(:order => 'RANDOM()', :select => 'id').id
  43 + end until @the_choice_ids.size == rank
  44 + logger.info "set populated and looks like #{@the_choice_ids.inspect}"
  45 + return @the_choice_ids.to_a
29 46 end
30 47  
31 48 def picked_prompt_id
... ...