title: Rails scope with polymorphic model
body: <p>I am getting some strange results from a very basic scope on a model with a polymorphic relationship.  Here's the brief summary and detail of the relationships.</p>
  
  <p>models/financials.rb</p>
  
  <pre><code>class Financial &lt; ActiveRecord::Base    
    belongs_to :financiable, :polymorphic =&gt; true
    #ltm is a boolean field in the model
    scope :ltm,  -&gt; { where(ltm: true).last }
  </code></pre>
  
  <p>And then there's a basic Firm model that has many financials</p>
  
  <p>models/firm.rb</p>
  
  <pre><code>class Firm &lt; ActiveRecord::Base
    has_many :financials, :as =&gt; :financiable, dependent: :destroy
  </code></pre>
  
  <p>So I get a bizarre result when a <code>firm</code> has no ltm financials (i.e. no financials with <code>ltm: true</code>).  But when I call <code>firm.financials.ltm</code>  I get an activerecord relation of financials that belong to the firm but do NOT have ltm: true.  However, when I just do <code>firm.financials.where(ltm: true).last</code> I get <code>nil</code></p>
  
  <p>Summary of results for when there are no ltm financials for the firm:</p>
  
  <pre><code>firm.financials.ltm #AR relation of financials that belong to the firm but are not ltm
  firm.financials.where(ltm: true).last #nil
  </code></pre>
  
  <p>And what makes it even stranger is that when a firm does have ltm financials, the scope works as expected.  </p>
  
  <p>Has anyone ever had this problem before or have any ideas?  I mean the easy answer is to not use the scope but I wanted to understand what could be causing this.</p>
  
  <p><strong>---UPDATES BASED ON COMMENTS---</strong>
  Thank you guys for putting a lot of thought into this. </p>
  
  <ol>
  <li>D-side You were correct.  The code was <code>firm.financials.ltm</code> and not <code>firm.financials.ltm.last</code>.  That was a typo when I typed up the question. I updated the above to reflect and also below are the SQL queries.</li>
  <li>Jiří Pospíšil - Great advice.  I will update in my app but leave the same here so as not to create confusion.  </li>
  <li>Chumakoff.  I force ltm to false if the user doesn't enter true with a before_save call so I don't think this is it but thanks for the thought.</li>
  </ol>
  
  <p>So these are from the scenario where <code>firm</code> doesn't have any <code>financials</code> with ltm = true.  As you can see, the scope request is making a second query to the database for all financials belonging to firm.  Why is it doing that when it can't find it in the initial query?</p>
  
  <blockquote>
    <p>firm.financials.ltm</p>
    
    <p>Financial Load (4.6ms)  SELECT  "financials".* FROM "financials" 
    WHERE "financials"."financiable_id" = $1 AND
    "financials"."financiable_type" = $2 AND "financials"."ltm" = 't' 
    ORDER BY "financials"."id" DESC LIMIT 1  [["financiable_id", 11],
    ["financiable_type", "Firm"]]<br>
    Financial Load (1.2ms)  SELECT "financials".* FROM "financials"  WHERE
    "financials"."financiable_id"= $1 AND "financials"."financiable_type"
    = $2  [["financiable_id", 11], ["financiable_type", "Firm"]]</p>
    
    <p>firm.financials.where(ltm: true).last</p>
    
    <p>Financial Load (16.8ms)  SELECT  "financials".* FROM "financials" 
    WHERE "financials"."financiable_id" = $1 AND
    "financials"."financiable_type" = $2 AND "financials"."ltm" = 't' 
    ORDER BY "financials"."id" DESC LIMIT 1  [["financiable_id", 11],
    ["financiable_type", "Firm"]]</p>
  </blockquote>