title: rails ActiveRecord includes method with where : where not applied in subsequent calls to get associations
body: <p>Say I have 2 tables <em>as</em> and <em>bs</em> in a many-to-many relationship with a join table. The join table has an attribute <em>cond</em>.</p>
  
  <p>The records are as follows:</p>
  
  <p>as:</p>
  
  <pre><code>id
  1
  </code></pre>
  
  <p>abs:</p>
  
  <pre><code>id  a_id  b_id  cond
  1   1     1     1
  2   1     1     0
  </code></pre>
  
  <p>bs:</p>
  
  <pre><code>id
  1
  </code></pre>
  
  <p>Now I do:</p>
  
  <blockquote>
    <p>b = A.find(1).bs.where(:abs => {:cond => 1}).includes(:abs).first</p>
  </blockquote>
  
  <p>Which gives me my <em>b</em> instance, the question is, why does <em>b.abs</em> return <strong>all</strong> <em>abs</em> rows?</p>
  
  <p>I thought the <em>includes</em> method was meant to avoid reloading associations in subsequent calls to the associations methods. Since I used a <em>where</em> in the first call I would expect <em>b.abs</em> to return only the row with <em>cond == 1</em>.</p>
  
  <p>I tried this in the rails console and no SQL request is issued when I perform the <em>b.abs</em> call. What is happening here? Is there a way to do what I want or do I need to put the condition again on <em>b.abs</em> like *b.abs.where(:abs => {:cond => 1}) ?</p>
  
  <p>As requested in the comments, here is the code for the models:</p>
  
  <p>cat app/models/a.rb</p>
  
  <pre><code>class A &lt; ActiveRecord::Base
      has_many :bs, :through =&gt; :abs
      has_many :abs   
  end
  </code></pre>
  
  <p>cat app/models/b.rb</p>
  
  <pre><code>class B &lt; ActiveRecord::Base
      has_many :as, :through =&gt; :abs
      has_many :abs 
  end
  </code></pre>
  
  <p>cat app/models/ab.rb                                                                              </p>
  
  <pre><code>class Ab &lt; ActiveRecord::Base
      attr_accessible :cond, :a_id, :b_id
      belongs_to :a
      belongs_to :b
  end
  </code></pre>