Commit 4c4c60d80c67a1aab90ecda5b24824e54c1e44cf

Authored by Pius Uzamere
1 parent 43a5f214

moar eager loading

app/controllers/choices_controller.rb
@@ -9,14 +9,9 @@ class ChoicesController < InheritedResources::Base @@ -9,14 +9,9 @@ class ChoicesController < InheritedResources::Base
9 @question = Question.find(params[:question_id])#, :include => :choices) 9 @question = Question.find(params[:question_id])#, :include => :choices)
10 @question.reload 10 @question.reload
11 @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item) 11 @choices = Choice.find(:all, :conditions => {:question_id => @question.id, :active => true}, :limit => params[:limit].to_i, :order => 'score DESC', :include => :item)
12 -  
13 - #@choices.each {|c| c.compute_score!}  
14 else 12 else
15 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices 13 @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices
16 @choices = @question.choices(true).active.find(:all, :include => :item) 14 @choices = @question.choices(true).active.find(:all, :include => :item)
17 -  
18 -  
19 - #@choices.each {|c| c.compute_score!}  
20 end 15 end
21 index! do |format| 16 index! do |format|
22 format.xml { render :xml => params[:data].blank? ? @choices.to_xml(:methods => [:item_data, :votes_count]) : @choices.to_xml(:include => [:items], :methods => [:data, :votes_count])} 17 format.xml { render :xml => params[:data].blank? ? @choices.to_xml(:methods => [:item_data, :votes_count]) : @choices.to_xml(:include => [:items], :methods => [:data, :votes_count])}
app/controllers/prompts_controller.rb
@@ -50,7 +50,7 @@ class PromptsController < InheritedResources::Base @@ -50,7 +50,7 @@ class PromptsController < InheritedResources::Base
50 50
51 logger.info "#{current_user.inspect} is voting #{direction}." 51 logger.info "#{current_user.inspect} is voting #{direction}."
52 @question = Question.find(params[:question_id]) 52 @question = Question.find(params[:question_id])
53 - @prompt = @question.prompts.find(params[:id]) 53 + @prompt = @question.prompts.find(params[:id], :include => :choices)
54 case direction 54 case direction
55 when :left 55 when :left
56 successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0) 56 successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0)
@@ -81,11 +81,11 @@ class PromptsController < InheritedResources::Base @@ -81,11 +81,11 @@ class PromptsController < InheritedResources::Base
81 @prompt = @question.prompts.find(params[:id]) 81 @prompt = @question.prompts.find(params[:id])
82 respond_to do |format| 82 respond_to do |format|
83 if @prompt.suspend! 83 if @prompt.suspend!
84 - format.xml { render :xml => @choice.to_xml, :status => :created }  
85 - format.json { render :json => @choice.to_json, :status => :created } 84 + format.xml { render :xml => @prompt.to_xml, :status => :created }
  85 + format.json { render :json => @prompt.to_json, :status => :created }
86 else 86 else
87 - format.xml { render :xml => @choice.errors, :status => :unprocessable_entity }  
88 - format.json { render :json => @choice.to_json } 87 + format.xml { render :xml => @prompt.errors, :status => :unprocessable_entity }
  88 + format.json { render :json => @prompt.to_json }
89 end 89 end
90 end 90 end
91 end 91 end
@@ -96,7 +96,7 @@ class PromptsController < InheritedResources::Base @@ -96,7 +96,7 @@ class PromptsController < InheritedResources::Base
96 authenticate 96 authenticate
97 logger.info "#{current_user.inspect} is skipping." 97 logger.info "#{current_user.inspect} is skipping."
98 @question = Question.find(params[:question_id]) 98 @question = Question.find(params[:question_id])
99 - @prompt = @question.prompts.find(params[:id]) 99 + @prompt = @question.prompts.find(params[:id], :include => [{ :left_choice => :item }, { :right_choice => :item }])
100 100
101 101
102 respond_to do |format| 102 respond_to do |format|
@@ -152,8 +152,7 @@ class PromptsController < InheritedResources::Base @@ -152,8 +152,7 @@ class PromptsController < InheritedResources::Base
152 152
153 def show 153 def show
154 @question = Question.find(params[:question_id]) 154 @question = Question.find(params[:question_id])
155 - @prompt = @question.prompts.find(params[:id])  
156 - show! do |format| 155 + @prompt = @question.prompts.find(params[:id], :include => [{ :left_choice => :item }, { :right_choice => :item }])
157 format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])} 156 format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])}
158 format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])} 157 format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])}
159 end 158 end
app/models/question.rb
@@ -23,7 +23,7 @@ class Question < ActiveRecord::Base @@ -23,7 +23,7 @@ class Question < ActiveRecord::Base
23 23
24 def picked_prompt 24 def picked_prompt
25 begin 25 begin
26 - @p = prompts.first(:order => 'RANDOM()') 26 + @p = prompts.first(:order => 'RANDOM()', :include => [{ :left_choice => :item }, { :right_choice => :item }])
27 end until @p.active? 27 end until @p.active?
28 return @p 28 return @p
29 end 29 end