title: Eager loading in Rails 3?
body: <p>I am attempting to learn how to increase the performance of Rails applications and the first step I am looking at is eager loading.</p>
  
  <p>I've configured the bullet gem which shows where I can make use of eager loading, however, I'm not too sure how to make use of the help!</p>
  
  <p>An example log is:</p>
  
  <pre><code>2012-04-26 15:59:34
  0.0.0.0:3000http://0.0.0.0:3000/animals
  N+1 Query detected
    Animal =&gt; [:client]
    Add to your finder: :include =&gt; [:client]
  N+1 Query method call stack
    N+1 Query method call stack
    /Users/dannymcclelland/Projects/premvet/app/views/animals/index.html.erb:26:in `block in _app_views_animals_index_html_erb__2796162405947806753_70316525286320'
    /Users/dannymcclelland/Projects/premvet/app/views/animals/index.html.erb:22:in `_app_views_animals_index_html_erb__2796162405947806753_70316525286320'
    /Users/dannymcclelland/Projects/premvet/app/controllers/animals_controller.rb:7:in `index'2012-04-26 15:59:34[WARN] user: dannymcclelland
  0.0.0.0:3000http://0.0.0.0:3000/animals
  Unused Eager Loading detected
    Animal =&gt; [:id, :AnimalName, :Species]
    Remove from your finder: :include =&gt; [:id, :AnimalName, :Species]2012-04-26 16:00:56
  </code></pre>
  
  <p>The lines that jump out are:</p>
  
  <pre><code>N+1 Query detected
  Animal =&gt; [:client]
  Add to your finder: :include =&gt; [:client]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Unused Eager Loading detected
  Animal =&gt; [:id, :AnimalName, :Species]
  Remove from your finder: :include =&gt; [:id, :AnimalName, :Species]
  </code></pre>
  
  <p>What I am unsure of, is what is the definition of the 'finder'. Is it the block in the view or is it in the controller.</p>
  
  <p>Taking the first section of log that jumps out at me the controller is as follows:</p>
  
  <pre><code>def index
  @animals = Animal.page(params[:page]).per_page(15)
  
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @animals }
  end
  end
  </code></pre>
  
  <p>the model:</p>
  
  <pre><code>class Animal &lt; ActiveRecord::Base
    self.table_name = 'animal'
    self.primary_key = 'PVID'
  
    attr_accessible :AddedBy, :Age, :AnimalBFAmount, :AnimalBalance, :AnimalName, :Archive,    :BillType, :Breed, :ChronicStatus, :Class, :Classification, :ClientKey, :Colour, :Date1, :DateOfBirth, :DateofBirth, :Dead, :DiaryQueue, :DiscField, :DrugsAtCost, :DrugsNoVAT, :ESDAmount, :ESDType, :FNote, :FirstRegisteredDate, :Height, :IDNumber, :Insured, :InsuredWith, :IsClient, :IsClientDate, :IsMaster, :LastBilledAmount, :LastBilledDate, :LastConsDate, :LastContributionDate, :LastPaidDate, :LastWeightDate, :Locked, :LoyaltyMultiplier, :LoyaltyPoints, :MR_Flag_0, :MR_Flag_1, :MR_Flag_10, :MR_Flag_11, :MR_Flag_12, :MR_Flag_13, :MR_Flag_14, :MR_Flag_15, :MR_Flag_2, :MR_Flag_3, :MR_Flag_4, :MR_Flag_5, :MR_Flag_6, :MR_Flag_7, :MR_Flag_7, :MR_Flag_8, :MR_Flag_9, :Mileage, :Neutered, :NextApptDate, :ORT, :OldSex, :Opt_Flag_0, :Opt_Flag_1, :Opt_Flag_2, :Opt_Flag_3, :Opt_Flag_4, :Opt_Flag_5, :Opt_Flag_6, :Opt_Flag_7, :PVID, :PreferredContact, :PreferredUser, :Ref1, :RefPrac, :ReferredBy, :SSDType, :SeenInPeriod, :SendBill, :Sex, :SiteAnimal, :Species, :Status, :SurcAmount, :SurcType, :SurgeryNumber, :TBU, :TOSAmount, :TOSDrugs, :TOSFees, :TOSType, :Weight
  
    belongs_to :client, :foreign_key =&gt; 'ClientKey'
    has_many :clinicals, :foreign_key =&gt; 'PVID'
    has_many :labs, :foreign_key =&gt; 'PVID'
    has_many :consults, :foreign_key =&gt; 'pvid'
    has_many :sheets, :foreign_key =&gt; 'PVID'
  
    default_scope :order =&gt; "Dead ASC, AnimalName ASC"
  end
  </code></pre>
  
  <p>Note that I'm using a legacy database hence the strange column names etc.</p>
  
  <p>The view contains the following:</p>
  
  <pre><code>&lt;% @animals.includes(:client).each do |animal| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= animal.id %&gt;&lt;/td&gt;    
      &lt;td&gt;&lt;%= animal.AnimalName %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to animal.client.Surname, animal.client %&gt;&lt;/td&gt;    
      &lt;td&gt;&lt;%= animal.Species %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= animal.Breed %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= animal.Dead %&gt;&lt;/td&gt;    
      &lt;td&gt;&lt;%= link_to 'Show', animal %&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>So, should I be adding the other recommended columns to the view 'finder'? Or somewhere else?</p>
  
  <p>Any help would be appreciated!</p>