我刚刚competitors_controller.rb
从...更改了一个动作
def audit
@review = Review.find(params[:review_id])
@competitor = @review.competitors.find(params[:id])
respond_with(@review, @competitor)
end
...至...
def audit
@review = Review.find(params[:review_id])
@competitor = Competitor.find(params[:id], :include => {:surveys => {:question => [:condition, :precondition]}})
respond_with(@review, @competitor)
end
...因为页面在加载时超时。
基本关联是:
class Competitor < ActiveRecord::Base
has_many :surveys
end
class Survey < ActiveRecord::Base
belongs_to :competitor
belongs_to :question
delegate :dependencies, :precondition, :condition, :to => :question
end
class Question < ActiveRecord::Base
has_many :dependancies, :class_name => "Question", :foreign_key => "precondition_id"
belongs_to :precondition, :class_name => "Question"
has_many :surveys, :dependent => :delete_all
end
基本上,audit.html.haml 页面加载:
@competitor.surveys.{sorting, etc}.each do |s|
s.foo, s.bar
s.{sorting, etc}.dependant_surveys.each do |s2|
s2.foo, s2.bar
s2.{sorting, etc}.dependant_surveys.each do |s3|
s3.foo, s3.bar, etc etc
如果我将它嵌套得太远,页面在超时之前不会加载。
我需要知道的是,我插入的预加载competitor_controllers.rb
理论上是否会加速以下两种方法中的每一种,这两种方法在构建时经常被调用audit.html.haml
?
class Survey < ActiveRecord::Base
def dependant_surveys
self.review.surveys.select{|survey| self.dependencies.include?(survey.question)}
end
end
class Question < ActiveRecord::Base
def dependencies
Question.all.select{|question| question.precondition == self}
end
end
(我说“理论上”,因为我知道这个问题也可以通过基准测试来回答。但在我走到那一步之前,我想检查一下我的理论是否正确。)