Commit 4c4c60d80c67a1aab90ecda5b24824e54c1e44cf
1 parent
43a5f214
Exists in
master
and in
1 other branch
moar eager loading
Showing
3 changed files
with
8 additions
and
14 deletions
Show diff stats
app/controllers/choices_controller.rb
... | ... | @@ -9,14 +9,9 @@ class ChoicesController < InheritedResources::Base |
9 | 9 | @question = Question.find(params[:question_id])#, :include => :choices) |
10 | 10 | @question.reload |
11 | 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 | 12 | else |
15 | 13 | @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices |
16 | 14 | @choices = @question.choices(true).active.find(:all, :include => :item) |
17 | - | |
18 | - | |
19 | - #@choices.each {|c| c.compute_score!} | |
20 | 15 | end |
21 | 16 | index! do |format| |
22 | 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 | 50 | |
51 | 51 | logger.info "#{current_user.inspect} is voting #{direction}." |
52 | 52 | @question = Question.find(params[:question_id]) |
53 | - @prompt = @question.prompts.find(params[:id]) | |
53 | + @prompt = @question.prompts.find(params[:id], :include => :choices) | |
54 | 54 | case direction |
55 | 55 | when :left |
56 | 56 | successful = c = current_user.record_vote(params['params']['auto'], @prompt, 0) |
... | ... | @@ -81,11 +81,11 @@ class PromptsController < InheritedResources::Base |
81 | 81 | @prompt = @question.prompts.find(params[:id]) |
82 | 82 | respond_to do |format| |
83 | 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 | 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 | 89 | end |
90 | 90 | end |
91 | 91 | end |
... | ... | @@ -96,7 +96,7 @@ class PromptsController < InheritedResources::Base |
96 | 96 | authenticate |
97 | 97 | logger.info "#{current_user.inspect} is skipping." |
98 | 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 | 102 | respond_to do |format| |
... | ... | @@ -152,8 +152,7 @@ class PromptsController < InheritedResources::Base |
152 | 152 | |
153 | 153 | def show |
154 | 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 | 156 | format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])} |
158 | 157 | format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])} |
159 | 158 | end | ... | ... |
app/models/question.rb
... | ... | @@ -23,7 +23,7 @@ class Question < ActiveRecord::Base |
23 | 23 | |
24 | 24 | def picked_prompt |
25 | 25 | begin |
26 | - @p = prompts.first(:order => 'RANDOM()') | |
26 | + @p = prompts.first(:order => 'RANDOM()', :include => [{ :left_choice => :item }, { :right_choice => :item }]) | |
27 | 27 | end until @p.active? |
28 | 28 | return @p |
29 | 29 | end | ... | ... |