Commit 26d83a875c8ba890f8c3fcfe886eee4efb55f7b2

Authored by Pius Uzamere
1 parent 968b9545

adding eager loading and getting rid of unnecessary score computation

app/controllers/prompts_controller.rb
@@ -61,7 +61,7 @@ class PromptsController < InheritedResources::Base @@ -61,7 +61,7 @@ class PromptsController < InheritedResources::Base
61 raise "need to specify either ':left' or ':right' as a direction" 61 raise "need to specify either ':left' or ':right' as a direction"
62 end 62 end
63 63
64 - @prompt.choices.each(&:compute_score!) 64 + #@prompt.choices.each(&:compute_score!)
65 respond_to do |format| 65 respond_to do |format|
66 if successful 66 if successful
67 format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok } 67 format.xml { render :xml => @question.picked_prompt.to_xml(:methods => [:left_choice_text, :right_choice_text, :left_choice_id, :right_choice_id]), :status => :ok }
app/models/question.rb
@@ -29,7 +29,7 @@ class Question < ActiveRecord::Base @@ -29,7 +29,7 @@ class Question < ActiveRecord::Base
29 def picked_prompt(rank = 2) 29 def picked_prompt(rank = 2)
30 raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2 30 raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2
31 choice_id_array = distinct_array_of_choice_ids(rank) 31 choice_id_array = distinct_array_of_choice_ids(rank)
32 - @p = prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1]) 32 + @p = prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1], :include => [{ :left_choice => :item }, { :right_choice => :item }])
33 end 33 end
34 memoize :picked_prompt 34 memoize :picked_prompt
35 35
app/models/visitor.rb
@@ -31,7 +31,7 @@ class Visitor < ActiveRecord::Base @@ -31,7 +31,7 @@ class Visitor < ActiveRecord::Base
31 # choice.score = choice.compute_score 31 # choice.score = choice.compute_score
32 # logger.info "Just computed the score for that choice and it's apparently #{choice.score}" 32 # logger.info "Just computed the score for that choice and it's apparently #{choice.score}"
33 # choice.save! 33 # choice.save!
34 - logger.info "Saved. That choice's score is still #{choice.score}" 34 + #logger.info "Saved. That choice's score is still #{choice.score}"
35 other_choices = choices - [choice] 35 other_choices = choices - [choice]
36 other_choices.each {|c| c.lose! } 36 other_choices.each {|c| c.lose! }
37 end 37 end