title: Rails 4 has_and_belongs_to_many doesn't work properly with includes statement
body: <p>Recently I ran into mystical bug with rails 4 and HABTM relation.
  first of all my Gemfile:</p>
  
  <pre><code>source 'https://rubygems.org'
  gem 'rails', '~&gt; 4.1.6'
  gem 'pg'
  </code></pre>
  
  <p>Next. my Models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  end
  
  class Teacher &lt; User
    has_and_belongs_to_many :resources, foreign_key: :user_id
  end
  
  class Resource &lt; ActiveRecord::Base
    has_and_belongs_to_many :teachers, association_foreign_key: :user_id
  end
  </code></pre>
  
  <p>Raw DB data:</p>
  
  <pre><code>select * from resources;
   id |         created_at         |         updated_at
  ----+----------------------------+----------------------------
    1 | 2014-10-13 08:24:07.308361 | 2014-10-13 08:24:07.308361
    2 | 2014-10-13 08:24:07.889907 | 2014-10-13 08:24:08.156898
    3 | 2014-10-13 08:24:08.68579  | 2014-10-13 08:24:08.884731
    4 | 2014-10-13 08:24:09.997244 | 2014-10-13 08:24:10.205753
  (4 rows)
  
  select * from users;
   id |         created_at         |         updated_at         |  type
  ----+----------------------------+----------------------------+---------
   13 | 2014-10-13 08:24:01.086192 | 2014-10-13 08:24:01.086192 | Teacher
   12 | 2014-10-13 08:24:00.984957 | 2014-10-13 08:24:00.984957 | Teacher
    2 | 2014-10-13 08:23:59.950349 | 2014-10-16 08:46:02.531245 | Teacher
  (3 rows)
  
  select * from resources_users;
   user_id | resource_id
  ---------+-------------
        13 |           1
         2 |           2
        12 |           3
         2 |           4
  (4 rows)
  </code></pre>
  
  <p><strong>Finally the bug:</strong></p>
  
  <pre><code>➜  rails_test  bundle exec rails c
  Loading development environment (Rails 4.1.6)
  2.1.2 :001 &gt; Resource.all.includes(:teachers).map(&amp;:teachers).map(&amp;:to_a)
    Resource Load (0.6ms)  SELECT "resources".* FROM "resources"
    SQL (1.3ms)  SELECT "resources_users".*, "resources_users"."user_id" AS t0_r0, "resources_users"."resource_id" AS t0_r1, "users"."id" AS t1_r0, "users"."created_at" AS t1_r1, "users"."updated_at" AS t1_r2, "users"."type" AS t1_r3 FROM "resources_users" LEFT OUTER JOIN "users" ON "users"."id" = "resources_users"."user_id" AND "users"."type" IN ('Teacher') WHERE "users"."type" IN ('Teacher') AND "resources_users"."resource_id" IN (1, 2, 3, 4)
   =&gt; [
  [#&lt;Teacher id: 13, created_at: "2014-10-13 08:24:01", updated_at: "2014-10-13 08:24:01", type: "Teacher"&gt;],
  [],
  [],
  []]
  </code></pre>
  
  <p>As you see only first array of teachers returned in collection. However SQL generated by Rails is correct and returns all data:</p>
  
  <pre><code>SELECT "resources_users".*, "resources_users"."user_id" AS t0_r0, "resources_users"."resource_id" AS t0_r1, "users"."id" AS t1_r0, "users"."created_at" AS t1_r1, "users"."updated_at" AS t1_r2, "users"."type" AS t1_r3 FROM "resources_users" LEFT OUTER JOIN "users" ON "users"."id" = "resources_users"."user_id" AND "users"."type" IN ('Teacher') WHERE "users"."type" IN ('Teacher') AND "resources_users"."resource_id" IN (1, 2, 3, 4);
   user_id | resource_id | t0_r0 | t0_r1 | t1_r0 |           t1_r1            |           t1_r2            |  t1_r3
  ---------+-------------+-------+-------+-------+----------------------------+----------------------------+---------
        13 |           1 |    13 |     1 |    13 | 2014-10-13 08:24:01.086192 | 2014-10-13 08:24:01.086192 | Teacher
         2 |           2 |     2 |     2 |     2 | 2014-10-13 08:23:59.950349 | 2014-10-16 08:46:02.531245 | Teacher
        12 |           3 |    12 |     3 |    12 | 2014-10-13 08:24:00.984957 | 2014-10-13 08:24:00.984957 | Teacher
         2 |           4 |     2 |     4 |     2 | 2014-10-13 08:23:59.950349 | 2014-10-16 08:46:02.531245 | Teacher
  (4 rows)
  </code></pre>
  
  <p>Did anyone faced such problem before? I can't understand what's going on here.</p>
  
  <p>P.S. If you do <code>Resource.all.includes(:teachers).map { |r| r.reload.teachers }</code> the result is correct. However it removes sense from <code>include</code> at all and provides N+1 problem.</p>
  
  <p>UPDATE: One more finding worth mentioning. If I remove STI everything works fine.</p>