<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -79,12 +79,22 @@ module ActiveRecord
         end
       end
 
-      def construct_id_map(records)
+      def construct_id_map(records, primary_key=nil)
+        # id_to_record_map = {}
+        # ids = []
+        # records.each do |record|
+        #   ids &lt;&lt; record.id
+        #   mapped_records = (id_to_record_map[record.id.to_s] ||= [])
+        #   mapped_records &lt;&lt; record
+        # end
+        # ids.uniq!
+        # return id_to_record_map, ids
         id_to_record_map = {}
         ids = []
         records.each do |record|
-          ids &lt;&lt; record.id
-          mapped_records = (id_to_record_map[record.id.to_s] ||= [])
+          primary_key ||= record.class.primary_key
+          ids &lt;&lt; record[primary_key]
+          mapped_records = (id_to_record_map[ids.last.to_s] ||= [])
           mapped_records &lt;&lt; record
         end
         ids.uniq!
@@ -131,55 +141,66 @@ module ActiveRecord
           set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
         end
       end
+      
+      
+      # class HasManyThroughAssociationPreloader
+      #   def initialize(records, reflection, options)
+      #     @records, @reflection, @options = records, reflection, options
+      #   end
+      #   
+      #   private 
+      #   attr_reader :records, :reflection, :options
+      # 
+      #   class HasAssociation
+      #   end
+      #   
+      #   class BelongsToAssociation
+      #   end
+      # end
+      
+
+      def preload_has_many_through_association(records, reflection, options, id_to_record_map)
+        # HasManyThroughAssociationPreloader.
+        
+        # -- 
+        through_records = preload_through_records(records, reflection, options[:through])
+        through_reflection = reflections[options[:through]]
+      
+        through_primary_key = through_reflection.primary_key_name
+        unless through_records.empty?
+          source = reflection.source_reflection.name
+          #add conditions from reflection!
+          through_records.first.class.preload_associations(through_records, source, reflection.options)
+          through_records.each do |through_record|
+            if through_reflection.macro == :belongs_to
+              through_record_id = through_record[through_record.class.primary_key].to_s
+            else
+              through_record_id = through_record[through_primary_key].to_s
+            end
+            add_preloaded_records_to_collection(
+              id_to_record_map[through_record_id],
+              reflection.name, 
+              through_record.send(source))
+          end
+        end
+      end
 
       def preload_has_many_association(records, reflection, preload_options={})
-        id_to_record_map, ids = construct_id_map(records)
-        records.each {|record| record.send(reflection.name).loaded}
         options = reflection.options
-
-        if options[:through]
-          through_records = preload_through_records(records, reflection, options[:through])
+        if reflection.options[:through]
           through_reflection = reflections[options[:through]]
-          
-          through_primary_key = through_reflection.primary_key_name
-          unless through_records.empty?
-            source = reflection.source_reflection.name
-            #add conditions from reflection!
-            through_records.first.class.preload_associations(through_records, source, reflection.options)
-            through_records.each do |through_record|
-              puts &quot;ID: #{id_to_record_map.inspect}&quot;
-              puts &quot;key: #{through_primary_key}&quot;
-              puts &quot;record: #{through_record.inspect}&quot;
-              puts &quot;id: #{through_record[through_primary_key].inspect}&quot;
-              if through_reflection.macro == :belongs_to
-
-# construct id map from belongs to
-        id_to_record_map = {}
-        ids = []
-        records.each do |record|
-          ids &lt;&lt; record.id
-          mapped_records = (id_to_record_map[record[through_primary_key].to_s] ||= [])
-          mapped_records &lt;&lt; record
+          if through_reflection.macro == :belongs_to
+            id_to_record_map, ids = construct_id_map(records, through_reflection.primary_key_name)
+          else
+            id_to_record_map, ids = construct_id_map(records)
+          end
+        else
+          id_to_record_map, ids = construct_id_map(records)
         end
-        ids.uniq!
+        records.each {|record| record.send(reflection.name).loaded}
 
-# stp 2
-through_record_id = through_record[through_record.class.primary_key]
-              puts
-              puts &quot;BELONGS TO -----------------&quot;
-              puts &quot;ID: #{id_to_record_map.inspect}&quot;
-              puts &quot;key: #{through_primary_key}&quot;
-              puts &quot;record: #{through_record.inspect}&quot;
-              puts &quot;id: #{through_record[through_primary_key].inspect}&quot;
-                
-                add_preloaded_records_to_collection(id_to_record_map[through_record_id.to_s],
-                                                 reflection.name, through_record.send(source))
-              else
-                add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_s],
-                                                 reflection.name, through_record.send(source))
-              end
-            end
-          end
+        if options[:through]
+          preload_has_many_through_association(records, reflection, reflection.options, id_to_record_map)
         else
           set_association_collection_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options),
                                              reflection.primary_key_name)</diff>
      <filename>activerecord/lib/active_record/association_preload.rb</filename>
    </modified>
    <modified>
      <diff>@@ -23,610 +23,610 @@ class EagerAssociationTest &lt; ActiveRecord::TestCase
             :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
             :developers, :projects
 
-  # def test_loading_with_one_association
-  #   posts = Post.find(:all, :include =&gt; :comments)
-  #   post = posts.find { |p| p.id == 1 }
-  #   assert_equal 2, post.comments.size
-  #   assert post.comments.include?(comments(:greetings))
-  # 
-  #   post = Post.find(:first, :include =&gt; :comments, :conditions =&gt; &quot;posts.title = 'Welcome to the weblog'&quot;)
-  #   assert_equal 2, post.comments.size
-  #   assert post.comments.include?(comments(:greetings))
-  # 
-  #   posts = Post.find(:all, :include =&gt; :last_comment)
-  #   post = posts.find { |p| p.id == 1 }
-  #   assert_equal Post.find(1).last_comment, post.last_comment
-  # end
-  # 
-  # def test_loading_conditions_with_or
-  #   posts = authors(:david).posts.find(:all, :include =&gt; :comments, :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'&quot;)
-  #   assert_nil posts.detect { |p| p.author_id != authors(:david).id },
-  #     &quot;expected to find only david's posts&quot;
-  # end
-  # 
-  # def test_with_ordering
-  #   list = Post.find(:all, :include =&gt; :comments, :order =&gt; &quot;posts.id DESC&quot;)
-  #   [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
-  #    :authorless, :thinking, :welcome
-  #   ].each_with_index do |post, index|
-  #     assert_equal posts(post), list[index]
-  #   end
-  # end
-  # 
-  # def test_with_two_tables_in_from_without_getting_double_quoted
-  #   posts = Post.find(:all,
-  #     :select     =&gt; &quot;posts.*&quot;,
-  #     :from       =&gt; &quot;authors, posts&quot;,
-  #     :include    =&gt; :comments,
-  #     :conditions =&gt; &quot;posts.author_id = authors.id&quot;,
-  #     :order      =&gt; &quot;posts.id&quot;
-  #   )
-  # 
-  #   assert_equal 2, posts.first.comments.size
-  # end
-  # 
-  # def test_loading_with_multiple_associations
-  #   posts = Post.find(:all, :include =&gt; [ :comments, :author, :categories ], :order =&gt; &quot;posts.id&quot;)
-  #   assert_equal 2, posts.first.comments.size
-  #   assert_equal 2, posts.first.categories.size
-  #   assert posts.first.comments.include?(comments(:greetings))
-  # end
-  # 
-  # def test_duplicate_middle_objects
-  #   comments = Comment.find :all, :conditions =&gt; 'post_id = 1', :include =&gt; [:post =&gt; :author]
-  #   assert_no_queries do
-  #     comments.each {|comment| comment.post.author.name}
-  #   end
-  # end
-  # 
-  # def test_including_duplicate_objects_from_belongs_to
-  #   popular_post = Post.create!(:title =&gt; 'foo', :body =&gt; &quot;I like cars!&quot;)
-  #   comment = popular_post.comments.create!(:body =&gt; &quot;lol&quot;)
-  #   popular_post.readers.create!(:person =&gt; people(:michael))
-  #   popular_post.readers.create!(:person =&gt; people(:david))
-  # 
-  #   readers = Reader.find(:all, :conditions =&gt; [&quot;post_id = ?&quot;, popular_post.id],
-  #                               :include =&gt; {:post =&gt; :comments})
-  #   readers.each do |reader|
-  #     assert_equal [comment], reader.post.comments
-  #   end
-  # end
-  # 
-  # def test_including_duplicate_objects_from_has_many
-  #   car_post = Post.create!(:title =&gt; 'foo', :body =&gt; &quot;I like cars!&quot;)
-  #   car_post.categories &lt;&lt; categories(:general)
-  #   car_post.categories &lt;&lt; categories(:technology)
-  # 
-  #   comment = car_post.comments.create!(:body =&gt; &quot;hmm&quot;)
-  #   categories = Category.find(:all, :conditions =&gt; [&quot;posts.id=?&quot;, car_post.id],
-  #                                :include =&gt; {:posts =&gt; :comments})
-  #   categories.each do |category|
-  #     assert_equal [comment], category.posts[0].comments
-  #   end
-  # end
-  # 
-  # def test_loading_from_an_association
-  #   posts = authors(:david).posts.find(:all, :include =&gt; :comments, :order =&gt; &quot;posts.id&quot;)
-  #   assert_equal 2, posts.first.comments.size
-  # end
-  # 
-  # def test_loading_with_no_associations
-  #   assert_nil Post.find(posts(:authorless).id, :include =&gt; :author).author
-  # end
-  # 
-  # def test_nested_loading_with_no_associations
-  #   assert_nothing_raised do
-  #     Post.find(posts(:authorless).id, :include =&gt; {:author =&gt; :author_addresss})
-  #   end
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_foreign_keys
-  #   pets = Pet.find(:all, :include =&gt; :owner)
-  #   assert_equal 3, pets.length
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to
-  #   comments = Comment.find(:all, :include =&gt; :post)
-  #   assert_equal 10, comments.length
-  #   titles = comments.map { |c| c.post.title }
-  #   assert titles.include?(posts(:welcome).title)
-  #   assert titles.include?(posts(:sti_post_and_comments).title)
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit
-  #   comments = Comment.find(:all, :include =&gt; :post, :limit =&gt; 5, :order =&gt; 'comments.id')
-  #   assert_equal 5, comments.length
-  #   assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
-  #   comments = Comment.find(:all, :include =&gt; :post, :conditions =&gt; 'post_id = 4', :limit =&gt; 3, :order =&gt; 'comments.id')
-  #   assert_equal 3, comments.length
-  #   assert_equal [5,6,7], comments.collect { |c| c.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit_and_offset
-  #   comments = Comment.find(:all, :include =&gt; :post, :limit =&gt; 3, :offset =&gt; 2, :order =&gt; 'comments.id')
-  #   assert_equal 3, comments.length
-  #   assert_equal [3,5,6], comments.collect { |c| c.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
-  #   comments = Comment.find(:all, :include =&gt; :post, :conditions =&gt; 'post_id = 4', :limit =&gt; 3, :offset =&gt; 1, :order =&gt; 'comments.id')
-  #   assert_equal 3, comments.length
-  #   assert_equal [6,7,8], comments.collect { |c| c.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
-  #   comments = Comment.find(:all, :include =&gt; :post, :conditions =&gt; ['post_id = ?',4], :limit =&gt; 3, :offset =&gt; 1, :order =&gt; 'comments.id')
-  #   assert_equal 3, comments.length
-  #   assert_equal [6,7,8], comments.collect { |c| c.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
-  #   assert_nothing_raised do
-  #     Comment.find(:all, :include =&gt; :post, :conditions =&gt; ['posts.id = ?',4])
-  #   end
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
-  #   quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
-  #   assert_nothing_raised do
-  #     Comment.find(:all, :include =&gt; :post, :conditions =&gt; [&quot;#{quoted_posts_id} = ?&quot;,4])
-  #   end
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
-  #   assert_nothing_raised do
-  #     Comment.find(:all, :include =&gt; :post, :order =&gt; 'posts.id')
-  #   end
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
-  #   quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
-  #   assert_nothing_raised do
-  #     Comment.find(:all, :include =&gt; :post, :order =&gt; quoted_posts_id)
-  #   end
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
-  #   posts = Post.find(:all, :include =&gt; [:author, :very_special_comment], :limit =&gt; 1, :order =&gt; 'posts.id')
-  #   assert_equal 1, posts.length
-  #   assert_equal [1], posts.collect { |p| p.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
-  #   posts = Post.find(:all, :include =&gt; [:author, :very_special_comment], :limit =&gt; 1, :offset =&gt; 1, :order =&gt; 'posts.id')
-  #   assert_equal 1, posts.length
-  #   assert_equal [2], posts.collect { |p| p.id }
-  # end
-  # 
-  # def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
-  #   author_favorite = AuthorFavorite.find(:first, :include =&gt; :favorite_author)
-  #   assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
-  # end
-  # 
-  # def test_eager_load_belongs_to_quotes_table_and_column_names
-  #   job = Job.find jobs(:unicyclist).id, :include =&gt; :ideal_reference
-  #   references(:michael_unicyclist)
-  #   assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
-  # end
-  # 
-  # def test_eager_load_has_one_quotes_table_and_column_names
-  #   michael = Person.find(people(:michael), :include =&gt; :favourite_reference)
-  #   references(:michael_unicyclist)
-  #   assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
-  # end
-  # 
-  # def test_eager_load_has_many_quotes_table_and_column_names
-  #   michael = Person.find(people(:michael), :include =&gt; :references)
-  #   references(:michael_magician,:michael_unicyclist)
-  #   assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&amp;:id) }
-  # end
-  # 
-  # def test_eager_load_has_many_through_quotes_table_and_column_names
-  #   michael = Person.find(people(:michael), :include =&gt; :jobs)
-  #   jobs(:magician, :unicyclist)
-  #   assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&amp;:id) }
-  # end
-  # 
-  # def test_eager_load_has_many_with_string_keys
-  #   subscriptions = subscriptions(:webster_awdr, :webster_rfr)
-  #   subscriber =Subscriber.find(subscribers(:second).id, :include =&gt; :subscriptions)
-  #   assert_equal subscriptions, subscriber.subscriptions.sort_by(&amp;:id)
-  # end
-  # 
-  # def test_eager_load_has_many_through_with_string_keys
-  #   books = books(:awdr, :rfr)
-  #   subscriber = Subscriber.find(subscribers(:second).id, :include =&gt; :books)
-  #   assert_equal books, subscriber.books.sort_by(&amp;:id)
-  # end
-  # 
-  # def test_eager_load_belongs_to_with_string_keys
-  #   subscriber = subscribers(:second)
-  #   subscription = Subscription.find(subscriptions(:webster_awdr).id, :include =&gt; :subscriber)
-  #   assert_equal subscriber, subscription.subscriber
-  # end
-  # 
-  # def test_eager_association_loading_with_explicit_join
-  #   posts = Post.find(:all, :include =&gt; :comments, :joins =&gt; &quot;INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'&quot;, :limit =&gt; 1, :order =&gt; 'author_id')
-  #   assert_equal 1, posts.length
-  # end
-  # 
-  # def test_eager_with_has_many_through
-  #   posts_with_comments = people(:michael).posts.find(:all, :include =&gt; :comments)
-  #   posts_with_author = people(:michael).posts.find(:all, :include =&gt; :author )
-  #   posts_with_comments_and_author = people(:michael).posts.find(:all, :include =&gt; [ :comments, :author ])
-  #   assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
-  #   assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
-  #   assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
-  # end
-
+  def test_loading_with_one_association
+    posts = Post.find(:all, :include =&gt; :comments)
+    post = posts.find { |p| p.id == 1 }
+    assert_equal 2, post.comments.size
+    assert post.comments.include?(comments(:greetings))
+  
+    post = Post.find(:first, :include =&gt; :comments, :conditions =&gt; &quot;posts.title = 'Welcome to the weblog'&quot;)
+    assert_equal 2, post.comments.size
+    assert post.comments.include?(comments(:greetings))
+  
+    posts = Post.find(:all, :include =&gt; :last_comment)
+    post = posts.find { |p| p.id == 1 }
+    assert_equal Post.find(1).last_comment, post.last_comment
+  end
+  
+  def test_loading_conditions_with_or
+    posts = authors(:david).posts.find(:all, :include =&gt; :comments, :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'&quot;)
+    assert_nil posts.detect { |p| p.author_id != authors(:david).id },
+      &quot;expected to find only david's posts&quot;
+  end
+  
+  def test_with_ordering
+    list = Post.find(:all, :include =&gt; :comments, :order =&gt; &quot;posts.id DESC&quot;)
+    [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
+     :authorless, :thinking, :welcome
+    ].each_with_index do |post, index|
+      assert_equal posts(post), list[index]
+    end
+  end
+  
+  def test_with_two_tables_in_from_without_getting_double_quoted
+    posts = Post.find(:all,
+      :select     =&gt; &quot;posts.*&quot;,
+      :from       =&gt; &quot;authors, posts&quot;,
+      :include    =&gt; :comments,
+      :conditions =&gt; &quot;posts.author_id = authors.id&quot;,
+      :order      =&gt; &quot;posts.id&quot;
+    )
+  
+    assert_equal 2, posts.first.comments.size
+  end
+  
+  def test_loading_with_multiple_associations
+    posts = Post.find(:all, :include =&gt; [ :comments, :author, :categories ], :order =&gt; &quot;posts.id&quot;)
+    assert_equal 2, posts.first.comments.size
+    assert_equal 2, posts.first.categories.size
+    assert posts.first.comments.include?(comments(:greetings))
+  end
+  
+  def test_duplicate_middle_objects
+    comments = Comment.find :all, :conditions =&gt; 'post_id = 1', :include =&gt; [:post =&gt; :author]
+    assert_no_queries do
+      comments.each {|comment| comment.post.author.name}
+    end
+  end
+  
+  def test_including_duplicate_objects_from_belongs_to
+    popular_post = Post.create!(:title =&gt; 'foo', :body =&gt; &quot;I like cars!&quot;)
+    comment = popular_post.comments.create!(:body =&gt; &quot;lol&quot;)
+    popular_post.readers.create!(:person =&gt; people(:michael))
+    popular_post.readers.create!(:person =&gt; people(:david))
+  
+    readers = Reader.find(:all, :conditions =&gt; [&quot;post_id = ?&quot;, popular_post.id],
+                                :include =&gt; {:post =&gt; :comments})
+    readers.each do |reader|
+      assert_equal [comment], reader.post.comments
+    end
+  end
+  
+  def test_including_duplicate_objects_from_has_many
+    car_post = Post.create!(:title =&gt; 'foo', :body =&gt; &quot;I like cars!&quot;)
+    car_post.categories &lt;&lt; categories(:general)
+    car_post.categories &lt;&lt; categories(:technology)
+  
+    comment = car_post.comments.create!(:body =&gt; &quot;hmm&quot;)
+    categories = Category.find(:all, :conditions =&gt; [&quot;posts.id=?&quot;, car_post.id],
+                                 :include =&gt; {:posts =&gt; :comments})
+    categories.each do |category|
+      assert_equal [comment], category.posts[0].comments
+    end
+  end
+  
+  def test_loading_from_an_association
+    posts = authors(:david).posts.find(:all, :include =&gt; :comments, :order =&gt; &quot;posts.id&quot;)
+    assert_equal 2, posts.first.comments.size
+  end
+  
+  def test_loading_with_no_associations
+    assert_nil Post.find(posts(:authorless).id, :include =&gt; :author).author
+  end
+  
+  def test_nested_loading_with_no_associations
+    assert_nothing_raised do
+      Post.find(posts(:authorless).id, :include =&gt; {:author =&gt; :author_addresss})
+    end
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_foreign_keys
+    pets = Pet.find(:all, :include =&gt; :owner)
+    assert_equal 3, pets.length
+  end
+  
+  def test_eager_association_loading_with_belongs_to
+    comments = Comment.find(:all, :include =&gt; :post)
+    assert_equal 10, comments.length
+    titles = comments.map { |c| c.post.title }
+    assert titles.include?(posts(:welcome).title)
+    assert titles.include?(posts(:sti_post_and_comments).title)
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit
+    comments = Comment.find(:all, :include =&gt; :post, :limit =&gt; 5, :order =&gt; 'comments.id')
+    assert_equal 5, comments.length
+    assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
+    comments = Comment.find(:all, :include =&gt; :post, :conditions =&gt; 'post_id = 4', :limit =&gt; 3, :order =&gt; 'comments.id')
+    assert_equal 3, comments.length
+    assert_equal [5,6,7], comments.collect { |c| c.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit_and_offset
+    comments = Comment.find(:all, :include =&gt; :post, :limit =&gt; 3, :offset =&gt; 2, :order =&gt; 'comments.id')
+    assert_equal 3, comments.length
+    assert_equal [3,5,6], comments.collect { |c| c.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
+    comments = Comment.find(:all, :include =&gt; :post, :conditions =&gt; 'post_id = 4', :limit =&gt; 3, :offset =&gt; 1, :order =&gt; 'comments.id')
+    assert_equal 3, comments.length
+    assert_equal [6,7,8], comments.collect { |c| c.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
+    comments = Comment.find(:all, :include =&gt; :post, :conditions =&gt; ['post_id = ?',4], :limit =&gt; 3, :offset =&gt; 1, :order =&gt; 'comments.id')
+    assert_equal 3, comments.length
+    assert_equal [6,7,8], comments.collect { |c| c.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
+    assert_nothing_raised do
+      Comment.find(:all, :include =&gt; :post, :conditions =&gt; ['posts.id = ?',4])
+    end
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
+    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
+    assert_nothing_raised do
+      Comment.find(:all, :include =&gt; :post, :conditions =&gt; [&quot;#{quoted_posts_id} = ?&quot;,4])
+    end
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
+    assert_nothing_raised do
+      Comment.find(:all, :include =&gt; :post, :order =&gt; 'posts.id')
+    end
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
+    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
+    assert_nothing_raised do
+      Comment.find(:all, :include =&gt; :post, :order =&gt; quoted_posts_id)
+    end
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
+    posts = Post.find(:all, :include =&gt; [:author, :very_special_comment], :limit =&gt; 1, :order =&gt; 'posts.id')
+    assert_equal 1, posts.length
+    assert_equal [1], posts.collect { |p| p.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
+    posts = Post.find(:all, :include =&gt; [:author, :very_special_comment], :limit =&gt; 1, :offset =&gt; 1, :order =&gt; 'posts.id')
+    assert_equal 1, posts.length
+    assert_equal [2], posts.collect { |p| p.id }
+  end
+  
+  def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
+    author_favorite = AuthorFavorite.find(:first, :include =&gt; :favorite_author)
+    assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
+  end
+  
+  def test_eager_load_belongs_to_quotes_table_and_column_names
+    job = Job.find jobs(:unicyclist).id, :include =&gt; :ideal_reference
+    references(:michael_unicyclist)
+    assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
+  end
+  
+  def test_eager_load_has_one_quotes_table_and_column_names
+    michael = Person.find(people(:michael), :include =&gt; :favourite_reference)
+    references(:michael_unicyclist)
+    assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
+  end
+  
+  def test_eager_load_has_many_quotes_table_and_column_names
+    michael = Person.find(people(:michael), :include =&gt; :references)
+    references(:michael_magician,:michael_unicyclist)
+    assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&amp;:id) }
+  end
+  
+  def test_eager_load_has_many_through_quotes_table_and_column_names
+    michael = Person.find(people(:michael), :include =&gt; :jobs)
+    jobs(:magician, :unicyclist)
+    assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&amp;:id) }
+  end
+  
+  def test_eager_load_has_many_with_string_keys
+    subscriptions = subscriptions(:webster_awdr, :webster_rfr)
+    subscriber =Subscriber.find(subscribers(:second).id, :include =&gt; :subscriptions)
+    assert_equal subscriptions, subscriber.subscriptions.sort_by(&amp;:id)
+  end
+  
+  def test_eager_load_has_many_through_with_string_keys
+    books = books(:awdr, :rfr)
+    subscriber = Subscriber.find(subscribers(:second).id, :include =&gt; :books)
+    assert_equal books, subscriber.books.sort_by(&amp;:id)
+  end
+  
+  def test_eager_load_belongs_to_with_string_keys
+    subscriber = subscribers(:second)
+    subscription = Subscription.find(subscriptions(:webster_awdr).id, :include =&gt; :subscriber)
+    assert_equal subscriber, subscription.subscriber
+  end
+  
+  def test_eager_association_loading_with_explicit_join
+    posts = Post.find(:all, :include =&gt; :comments, :joins =&gt; &quot;INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'&quot;, :limit =&gt; 1, :order =&gt; 'author_id')
+    assert_equal 1, posts.length
+  end
+  
+  def test_eager_with_has_many_through
+    posts_with_comments = people(:michael).posts.find(:all, :include =&gt; :comments)
+    posts_with_author = people(:michael).posts.find(:all, :include =&gt; :author )
+    posts_with_comments_and_author = people(:michael).posts.find(:all, :include =&gt; [ :comments, :author ])
+    assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
+    assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
+    assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
+  end
+  
   def test_eager_with_has_many_through_a_belongs_to_association
     author = authors(:mary)
     post = Post.create!(:author =&gt; author, :title =&gt; &quot;TITLE&quot;, :body =&gt; &quot;BODY&quot;)
     author.author_favorites.create(:favorite_author_id =&gt; 1)
     author.author_favorites.create(:favorite_author_id =&gt; 2)
     posts_with_author_favorites = author.posts.find(:all, :include =&gt; :author_favorites)
-    assert_no_queries { posts_with_author_favorites.first.author_favorites }
-  end
-  
-  # def test_eager_with_has_many_through_an_sti_join_model
-  #   author = Author.find(:first, :include =&gt; :special_post_comments, :order =&gt; 'authors.id')
-  #   assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
-  # end
-  # 
-  # def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
-  #   author = Author.find(:first, :include =&gt; :special_nonexistant_post_comments, :order =&gt; 'authors.id')
-  #   assert_equal [], author.special_nonexistant_post_comments
-  # end
-  # 
-  # def test_eager_with_has_many_through_join_model_with_conditions
-  #   assert_equal Author.find(:first, :include =&gt; :hello_post_comments,
-  #                            :order =&gt; 'authors.id').hello_post_comments.sort_by(&amp;:id),
-  #                Author.find(:first, :order =&gt; 'authors.id').hello_post_comments.sort_by(&amp;:id)
-  # end
-  # 
-  # def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
-  #   assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include =&gt; :comments_with_order_and_conditions).comments_with_order_and_conditions.first
-  # end
-  # 
-  # def test_eager_with_has_many_through_join_model_with_include
-  #   author_comments = Author.find(authors(:david).id, :include =&gt; :comments_with_include).comments_with_include.to_a
-  #   assert_no_queries do
-  #     author_comments.first.post.title
-  #   end
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit
-  #   posts = Post.find(:all, :order =&gt; 'posts.id asc', :include =&gt; [ :author, :comments ], :limit =&gt; 2)
-  #   assert_equal 2, posts.size
-  #   assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_and_conditions
-  #   if current_adapter?(:OpenBaseAdapter)
-  #     posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; &quot;FETCHBLOB(posts.body) = 'hello'&quot;, :order =&gt; &quot;posts.id&quot;)
-  #   else
-  #     posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; &quot;posts.body = 'hello'&quot;, :order =&gt; &quot;posts.id&quot;)
-  #   end
-  #   assert_equal 2, posts.size
-  #   assert_equal [4,5], posts.collect { |p| p.id }
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_and_conditions_array
-  #   if current_adapter?(:OpenBaseAdapter)
-  #     posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;FETCHBLOB(posts.body) = ?&quot;, 'hello' ], :order =&gt; &quot;posts.id&quot;)
-  #   else
-  #     posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;posts.body = ?&quot;, 'hello' ], :order =&gt; &quot;posts.id&quot;)
-  #   end
-  #   assert_equal 2, posts.size
-  #   assert_equal [4,5], posts.collect { |p| p.id }
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
-  #   posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
-  #   assert_equal 2, posts.size
-  # 
-  #   count = Post.count(:include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
-  #   assert_equal count, posts.size
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_ond_high_offset
-  #   posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :offset =&gt; 10, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
-  #   assert_equal 0, posts.size
-  # end
-  # 
-  # def test_count_eager_with_has_many_and_limit_ond_high_offset
-  #   posts = Post.count(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :offset =&gt; 10, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
-  #   assert_equal 0, posts
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_with_no_results
-  #   posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; &quot;posts.title = 'magic forest'&quot;)
-  #   assert_equal 0, posts.size
-  # end
-  # 
-  # def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
-  #   author = authors(:david)
-  #   author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
-  #   assert_equal author_posts_without_comments.size, author.posts.count(:all, :include =&gt; :comments, :conditions =&gt; 'comments.id is null')
-  # end
-  # 
-  # def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
-  #   person = people(:michael)
-  #   person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
-  #   assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
-  # end
-  # 
-  # def test_eager_with_has_and_belongs_to_many_and_limit
-  #   posts = Post.find(:all, :include =&gt; :categories, :order =&gt; &quot;posts.id&quot;, :limit =&gt; 3)
-  #   assert_equal 3, posts.size
-  #   assert_equal 2, posts[0].categories.size
-  #   assert_equal 1, posts[1].categories.size
-  #   assert_equal 0, posts[2].categories.size
-  #   assert posts[0].categories.include?(categories(:technology))
-  #   assert posts[1].categories.include?(categories(:general))
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
-  #   posts = authors(:david).posts.find(:all,
-  #     :include    =&gt; :comments,
-  #     :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'&quot;,
-  #     :limit      =&gt; 2
-  #   )
-  #   assert_equal 2, posts.size
-  # 
-  #   count = Post.count(
-  #     :include    =&gt; [ :comments, :author ],
-  #     :conditions =&gt; &quot;authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')&quot;,
-  #     :limit      =&gt; 2
-  #   )
-  #   assert_equal count, posts.size
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
-  #   posts = nil
-  #   Post.with_scope(:find =&gt; {
-  #     :include    =&gt; :comments,
-  #     :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'&quot;
-  #   }) do
-  #     posts = authors(:david).posts.find(:all, :limit =&gt; 2)
-  #     assert_equal 2, posts.size
-  #   end
-  # 
-  #   Post.with_scope(:find =&gt; {
-  #     :include    =&gt; [ :comments, :author ],
-  #     :conditions =&gt; &quot;authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')&quot;
-  #   }) do
-  #     count = Post.count(:limit =&gt; 2)
-  #     assert_equal count, posts.size
-  #   end
-  # end
-  # 
-  # def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
-  #   Post.with_scope(:find =&gt; { :conditions =&gt; &quot;1=1&quot; }) do
-  #     posts = authors(:david).posts.find(:all,
-  #       :include    =&gt; :comments,
-  #       :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'&quot;,
-  #       :limit      =&gt; 2
-  #     )
-  #     assert_equal 2, posts.size
-  # 
-  #     count = Post.count(
-  #       :include    =&gt; [ :comments, :author ],
-  #       :conditions =&gt; &quot;authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')&quot;,
-  #       :limit      =&gt; 2
-  #     )
-  #     assert_equal count, posts.size
-  #   end
-  # end
-  # 
-  # def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
-  #   posts_with_explicit_order = Post.find(:all, :conditions =&gt; 'comments.id is not null', :include =&gt; :comments, :order =&gt; 'posts.id DESC', :limit =&gt; 2)
-  #   posts_with_scoped_order = Post.with_scope(:find =&gt; {:order =&gt; 'posts.id DESC'}) do
-  #     Post.find(:all, :conditions =&gt; 'comments.id is not null', :include =&gt; :comments, :limit =&gt; 2)
-  #   end
-  #   assert_equal posts_with_explicit_order, posts_with_scoped_order
-  # end
-  # 
-  # def test_eager_association_loading_with_habtm
-  #   posts = Post.find(:all, :include =&gt; :categories, :order =&gt; &quot;posts.id&quot;)
-  #   assert_equal 2, posts[0].categories.size
-  #   assert_equal 1, posts[1].categories.size
-  #   assert_equal 0, posts[2].categories.size
-  #   assert posts[0].categories.include?(categories(:technology))
-  #   assert posts[1].categories.include?(categories(:general))
-  # end
-  # 
-  # def test_eager_with_inheritance
-  #   posts = SpecialPost.find(:all, :include =&gt; [ :comments ])
-  # end
-  # 
-  # def test_eager_has_one_with_association_inheritance
-  #   post = Post.find(4, :include =&gt; [ :very_special_comment ])
-  #   assert_equal &quot;VerySpecialComment&quot;, post.very_special_comment.class.to_s
-  # end
-  # 
-  # def test_eager_has_many_with_association_inheritance
-  #   post = Post.find(4, :include =&gt; [ :special_comments ])
-  #   post.special_comments.each do |special_comment|
-  #     assert_equal &quot;SpecialComment&quot;, special_comment.class.to_s
-  #   end
-  # end
-  # 
-  # def test_eager_habtm_with_association_inheritance
-  #   post = Post.find(6, :include =&gt; [ :special_categories ])
-  #   assert_equal 1, post.special_categories.size
-  #   post.special_categories.each do |special_category|
-  #     assert_equal &quot;SpecialCategory&quot;, special_category.class.to_s
-  #   end
-  # end
-  # 
-  # def test_eager_with_has_one_dependent_does_not_destroy_dependent
-  #   assert_not_nil companies(:first_firm).account
-  #   f = Firm.find(:first, :include =&gt; :account,
-  #           :conditions =&gt; [&quot;companies.name = ?&quot;, &quot;37signals&quot;])
-  #   assert_not_nil f.account
-  #   assert_equal companies(:first_firm, :reload).account, f.account
-  # end
-  # 
-  # def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
-  #   author = authors(:david)
-  #   posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
-  #   assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
-  #   assert_equal posts_with_no_comments, author.posts_with_no_comments
-  # end
-  # 
-  # def test_eager_with_invalid_association_reference
-  #   assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys&quot;) {
-  #     post = Post.find(6, :include=&gt; :monkeys )
-  #   }
-  #   assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys&quot;) {
-  #     post = Post.find(6, :include=&gt;[ :monkeys ])
-  #   }
-  #   assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys&quot;) {
-  #     post = Post.find(6, :include=&gt;[ 'monkeys' ])
-  #   }
-  #   assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys, :elephants&quot;) {
-  #     post = Post.find(6, :include=&gt;[ :monkeys, :elephants ])
-  #   }
-  # end
-  # 
-  # def find_all_ordered(className, include=nil)
-  #   className.find(:all, :order=&gt;&quot;#{className.table_name}.#{className.primary_key}&quot;, :include=&gt;include)
-  # end
-  # 
-  # def test_limited_eager_with_order
-  #   assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title)', :limit =&gt; 2, :offset =&gt; 1)
-  #   assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title) DESC', :limit =&gt; 2, :offset =&gt; 1)
-  # end
-  # 
-  # def test_limited_eager_with_multiple_order_columns
-  #   assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title), posts.id', :limit =&gt; 2, :offset =&gt; 1)
-  #   assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title) DESC, posts.id', :limit =&gt; 2, :offset =&gt; 1)
-  # end
-  # 
-  # def test_preload_with_interpolation
-  #   assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include =&gt; :comments_with_interpolated_conditions).comments_with_interpolated_conditions
-  # end
-  # 
-  # def test_polymorphic_type_condition
-  #   post = Post.find(posts(:thinking).id, :include =&gt; :taggings)
-  #   assert post.taggings.include?(taggings(:thinking_general))
-  #   post = SpecialPost.find(posts(:thinking).id, :include =&gt; :taggings)
-  #   assert post.taggings.include?(taggings(:thinking_general))
-  # end
-  # 
-  # def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
-  #   # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
-  #   def assert_equal_after_sort(item1, item2, item3 = nil)
-  #     assert_equal(item1.sort{|a,b| a.id &lt;=&gt; b.id}, item2.sort{|a,b| a.id &lt;=&gt; b.id})
-  #     assert_equal(item3.sort{|a,b| a.id &lt;=&gt; b.id}, item2.sort{|a,b| a.id &lt;=&gt; b.id}) if item3
-  #   end
-  #   # Test regular association, association with conditions, association with
-  #   # STI, and association with conditions assured not to be true
-  #   post_types = [:posts, :other_posts, :special_posts]
-  #   # test both has_many and has_and_belongs_to_many
-  #   [Author, Category].each do |className|
-  #     d1 = find_all_ordered(className)
-  #     # test including all post types at once
-  #     d2 = find_all_ordered(className, post_types)
-  #     d1.each_index do |i|
-  #       assert_equal(d1[i], d2[i])
-  #       assert_equal_after_sort(d1[i].posts, d2[i].posts)
-  #       post_types[1..-1].each do |post_type|
-  #         # test including post_types together
-  #         d3 = find_all_ordered(className, [:posts, post_type])
-  #         assert_equal(d1[i], d3[i])
-  #         assert_equal_after_sort(d1[i].posts, d3[i].posts)
-  #         assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
-  #       end
-  #     end
-  #   end
-  # end
-  # 
-  # def test_eager_with_multiple_associations_with_same_table_has_one
-  #   d1 = find_all_ordered(Firm)
-  #   d2 = find_all_ordered(Firm, :account)
-  #   d1.each_index do |i|
-  #     assert_equal(d1[i], d2[i])
-  #     assert_equal(d1[i].account, d2[i].account)
-  #   end
-  # end
-  # 
-  # def test_eager_with_multiple_associations_with_same_table_belongs_to
-  #   firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
-  #   d1 = find_all_ordered(Client)
-  #   d2 = find_all_ordered(Client, firm_types)
-  #   d1.each_index do |i|
-  #     assert_equal(d1[i], d2[i])
-  #     firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
-  #   end
-  # end
-  # def test_eager_with_valid_association_as_string_not_symbol
-  #   assert_nothing_raised { Post.find(:all, :include =&gt; 'comments') }
-  # end
-  # 
-  # def test_preconfigured_includes_with_belongs_to
-  #   author = posts(:welcome).author_with_posts
-  #   assert_no_queries {assert_equal 5, author.posts.size}
-  # end
-  # 
-  # def test_preconfigured_includes_with_has_one
-  #   comment = posts(:sti_comments).very_special_comment_with_post
-  #   assert_no_queries {assert_equal posts(:sti_comments), comment.post}
-  # end
-  # 
-  # def test_preconfigured_includes_with_has_many
-  #   posts = authors(:david).posts_with_comments
-  #   one = posts.detect { |p| p.id == 1 }
-  #   assert_no_queries do
-  #     assert_equal 5, posts.size
-  #     assert_equal 2, one.comments.size
-  #   end
-  # end
-  # 
-  # def test_preconfigured_includes_with_habtm
-  #   posts = authors(:david).posts_with_categories
-  #   one = posts.detect { |p| p.id == 1 }
-  #   assert_no_queries do
-  #     assert_equal 5, posts.size
-  #     assert_equal 2, one.categories.size
-  #   end
-  # end
-  # 
-  # def test_preconfigured_includes_with_has_many_and_habtm
-  #   posts = authors(:david).posts_with_comments_and_categories
-  #   one = posts.detect { |p| p.id == 1 }
-  #   assert_no_queries do
-  #     assert_equal 5, posts.size
-  #     assert_equal 2, one.comments.size
-  #     assert_equal 2, one.categories.size
-  #   end
-  # end
-  # 
-  # def test_count_with_include
-  #   if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
-  #     assert_equal 3, authors(:david).posts_with_comments.count(:conditions =&gt; &quot;len(comments.body) &gt; 15&quot;)
-  #   elsif current_adapter?(:OpenBaseAdapter)
-  #     assert_equal 3, authors(:david).posts_with_comments.count(:conditions =&gt; &quot;length(FETCHBLOB(comments.body)) &gt; 15&quot;)
-  #   else
-  #     assert_equal 3, authors(:david).posts_with_comments.count(:conditions =&gt; &quot;length(comments.body) &gt; 15&quot;)
-  #   end
-  # end
-  # 
-  # def test_load_with_sti_sharing_association
-  #   assert_queries(2) do #should not do 1 query per subclass
-  #     Comment.find :all, :include =&gt; :post
-  #   end
-  # end
-  # 
-  # def test_conditions_on_join_table_with_include_and_limit
-  #   assert_equal 3, Developer.find(:all, :include =&gt; 'projects', :conditions =&gt; 'developers_projects.access_level = 1', :limit =&gt; 5).size
-  # end
-  # 
-  # def test_order_on_join_table_with_include_and_limit
-  #   assert_equal 5, Developer.find(:all, :include =&gt; 'projects', :order =&gt; 'developers_projects.joined_on DESC', :limit =&gt; 5).size
-  # end
+    assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
+  end
+  
+  def test_eager_with_has_many_through_an_sti_join_model
+    author = Author.find(:first, :include =&gt; :special_post_comments, :order =&gt; 'authors.id')
+    assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
+  end
+  
+  def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
+    author = Author.find(:first, :include =&gt; :special_nonexistant_post_comments, :order =&gt; 'authors.id')
+    assert_equal [], author.special_nonexistant_post_comments
+  end
+  
+  def test_eager_with_has_many_through_join_model_with_conditions
+    assert_equal Author.find(:first, :include =&gt; :hello_post_comments,
+                             :order =&gt; 'authors.id').hello_post_comments.sort_by(&amp;:id),
+                 Author.find(:first, :order =&gt; 'authors.id').hello_post_comments.sort_by(&amp;:id)
+  end
+  
+  def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
+    assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include =&gt; :comments_with_order_and_conditions).comments_with_order_and_conditions.first
+  end
+  
+  def test_eager_with_has_many_through_join_model_with_include
+    author_comments = Author.find(authors(:david).id, :include =&gt; :comments_with_include).comments_with_include.to_a
+    assert_no_queries do
+      author_comments.first.post.title
+    end
+  end
+  
+  def test_eager_with_has_many_and_limit
+    posts = Post.find(:all, :order =&gt; 'posts.id asc', :include =&gt; [ :author, :comments ], :limit =&gt; 2)
+    assert_equal 2, posts.size
+    assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
+  end
+  
+  def test_eager_with_has_many_and_limit_and_conditions
+    if current_adapter?(:OpenBaseAdapter)
+      posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; &quot;FETCHBLOB(posts.body) = 'hello'&quot;, :order =&gt; &quot;posts.id&quot;)
+    else
+      posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; &quot;posts.body = 'hello'&quot;, :order =&gt; &quot;posts.id&quot;)
+    end
+    assert_equal 2, posts.size
+    assert_equal [4,5], posts.collect { |p| p.id }
+  end
+  
+  def test_eager_with_has_many_and_limit_and_conditions_array
+    if current_adapter?(:OpenBaseAdapter)
+      posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;FETCHBLOB(posts.body) = ?&quot;, 'hello' ], :order =&gt; &quot;posts.id&quot;)
+    else
+      posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;posts.body = ?&quot;, 'hello' ], :order =&gt; &quot;posts.id&quot;)
+    end
+    assert_equal 2, posts.size
+    assert_equal [4,5], posts.collect { |p| p.id }
+  end
+  
+  def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
+    posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
+    assert_equal 2, posts.size
+  
+    count = Post.count(:include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
+    assert_equal count, posts.size
+  end
+  
+  def test_eager_with_has_many_and_limit_ond_high_offset
+    posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :offset =&gt; 10, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
+    assert_equal 0, posts.size
+  end
+  
+  def test_count_eager_with_has_many_and_limit_ond_high_offset
+    posts = Post.count(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :offset =&gt; 10, :conditions =&gt; [ &quot;authors.name = ?&quot;, 'David' ])
+    assert_equal 0, posts
+  end
+  
+  def test_eager_with_has_many_and_limit_with_no_results
+    posts = Post.find(:all, :include =&gt; [ :author, :comments ], :limit =&gt; 2, :conditions =&gt; &quot;posts.title = 'magic forest'&quot;)
+    assert_equal 0, posts.size
+  end
+  
+  def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
+    author = authors(:david)
+    author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
+    assert_equal author_posts_without_comments.size, author.posts.count(:all, :include =&gt; :comments, :conditions =&gt; 'comments.id is null')
+  end
+  
+  def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
+    person = people(:michael)
+    person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
+    assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
+  end
+  
+  def test_eager_with_has_and_belongs_to_many_and_limit
+    posts = Post.find(:all, :include =&gt; :categories, :order =&gt; &quot;posts.id&quot;, :limit =&gt; 3)
+    assert_equal 3, posts.size
+    assert_equal 2, posts[0].categories.size
+    assert_equal 1, posts[1].categories.size
+    assert_equal 0, posts[2].categories.size
+    assert posts[0].categories.include?(categories(:technology))
+    assert posts[1].categories.include?(categories(:general))
+  end
+  
+  def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
+    posts = authors(:david).posts.find(:all,
+      :include    =&gt; :comments,
+      :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'&quot;,
+      :limit      =&gt; 2
+    )
+    assert_equal 2, posts.size
+  
+    count = Post.count(
+      :include    =&gt; [ :comments, :author ],
+      :conditions =&gt; &quot;authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')&quot;,
+      :limit      =&gt; 2
+    )
+    assert_equal count, posts.size
+  end
+  
+  def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
+    posts = nil
+    Post.with_scope(:find =&gt; {
+      :include    =&gt; :comments,
+      :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'&quot;
+    }) do
+      posts = authors(:david).posts.find(:all, :limit =&gt; 2)
+      assert_equal 2, posts.size
+    end
+  
+    Post.with_scope(:find =&gt; {
+      :include    =&gt; [ :comments, :author ],
+      :conditions =&gt; &quot;authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')&quot;
+    }) do
+      count = Post.count(:limit =&gt; 2)
+      assert_equal count, posts.size
+    end
+  end
+  
+  def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
+    Post.with_scope(:find =&gt; { :conditions =&gt; &quot;1=1&quot; }) do
+      posts = authors(:david).posts.find(:all,
+        :include    =&gt; :comments,
+        :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'&quot;,
+        :limit      =&gt; 2
+      )
+      assert_equal 2, posts.size
+  
+      count = Post.count(
+        :include    =&gt; [ :comments, :author ],
+        :conditions =&gt; &quot;authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')&quot;,
+        :limit      =&gt; 2
+      )
+      assert_equal count, posts.size
+    end
+  end
+  
+  def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
+    posts_with_explicit_order = Post.find(:all, :conditions =&gt; 'comments.id is not null', :include =&gt; :comments, :order =&gt; 'posts.id DESC', :limit =&gt; 2)
+    posts_with_scoped_order = Post.with_scope(:find =&gt; {:order =&gt; 'posts.id DESC'}) do
+      Post.find(:all, :conditions =&gt; 'comments.id is not null', :include =&gt; :comments, :limit =&gt; 2)
+    end
+    assert_equal posts_with_explicit_order, posts_with_scoped_order
+  end
+  
+  def test_eager_association_loading_with_habtm
+    posts = Post.find(:all, :include =&gt; :categories, :order =&gt; &quot;posts.id&quot;)
+    assert_equal 2, posts[0].categories.size
+    assert_equal 1, posts[1].categories.size
+    assert_equal 0, posts[2].categories.size
+    assert posts[0].categories.include?(categories(:technology))
+    assert posts[1].categories.include?(categories(:general))
+  end
+  
+  def test_eager_with_inheritance
+    posts = SpecialPost.find(:all, :include =&gt; [ :comments ])
+  end
+  
+  def test_eager_has_one_with_association_inheritance
+    post = Post.find(4, :include =&gt; [ :very_special_comment ])
+    assert_equal &quot;VerySpecialComment&quot;, post.very_special_comment.class.to_s
+  end
+  
+  def test_eager_has_many_with_association_inheritance
+    post = Post.find(4, :include =&gt; [ :special_comments ])
+    post.special_comments.each do |special_comment|
+      assert_equal &quot;SpecialComment&quot;, special_comment.class.to_s
+    end
+  end
+  
+  def test_eager_habtm_with_association_inheritance
+    post = Post.find(6, :include =&gt; [ :special_categories ])
+    assert_equal 1, post.special_categories.size
+    post.special_categories.each do |special_category|
+      assert_equal &quot;SpecialCategory&quot;, special_category.class.to_s
+    end
+  end
+  
+  def test_eager_with_has_one_dependent_does_not_destroy_dependent
+    assert_not_nil companies(:first_firm).account
+    f = Firm.find(:first, :include =&gt; :account,
+            :conditions =&gt; [&quot;companies.name = ?&quot;, &quot;37signals&quot;])
+    assert_not_nil f.account
+    assert_equal companies(:first_firm, :reload).account, f.account
+  end
+  
+  def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
+    author = authors(:david)
+    posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
+    assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
+    assert_equal posts_with_no_comments, author.posts_with_no_comments
+  end
+  
+  def test_eager_with_invalid_association_reference
+    assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys&quot;) {
+      post = Post.find(6, :include=&gt; :monkeys )
+    }
+    assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys&quot;) {
+      post = Post.find(6, :include=&gt;[ :monkeys ])
+    }
+    assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys&quot;) {
+      post = Post.find(6, :include=&gt;[ 'monkeys' ])
+    }
+    assert_raises(ActiveRecord::ConfigurationError, &quot;Association was not found; perhaps you misspelled it?  You specified :include =&gt; :monkeys, :elephants&quot;) {
+      post = Post.find(6, :include=&gt;[ :monkeys, :elephants ])
+    }
+  end
+  
+  def find_all_ordered(className, include=nil)
+    className.find(:all, :order=&gt;&quot;#{className.table_name}.#{className.primary_key}&quot;, :include=&gt;include)
+  end
+  
+  def test_limited_eager_with_order
+    assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title)', :limit =&gt; 2, :offset =&gt; 1)
+    assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title) DESC', :limit =&gt; 2, :offset =&gt; 1)
+  end
+  
+  def test_limited_eager_with_multiple_order_columns
+    assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title), posts.id', :limit =&gt; 2, :offset =&gt; 1)
+    assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include =&gt; [:author, :comments], :conditions =&gt; &quot;authors.name = 'David'&quot;, :order =&gt; 'UPPER(posts.title) DESC, posts.id', :limit =&gt; 2, :offset =&gt; 1)
+  end
+  
+  def test_preload_with_interpolation
+    assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include =&gt; :comments_with_interpolated_conditions).comments_with_interpolated_conditions
+  end
+  
+  def test_polymorphic_type_condition
+    post = Post.find(posts(:thinking).id, :include =&gt; :taggings)
+    assert post.taggings.include?(taggings(:thinking_general))
+    post = SpecialPost.find(posts(:thinking).id, :include =&gt; :taggings)
+    assert post.taggings.include?(taggings(:thinking_general))
+  end
+  
+  def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
+    # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
+    def assert_equal_after_sort(item1, item2, item3 = nil)
+      assert_equal(item1.sort{|a,b| a.id &lt;=&gt; b.id}, item2.sort{|a,b| a.id &lt;=&gt; b.id})
+      assert_equal(item3.sort{|a,b| a.id &lt;=&gt; b.id}, item2.sort{|a,b| a.id &lt;=&gt; b.id}) if item3
+    end
+    # Test regular association, association with conditions, association with
+    # STI, and association with conditions assured not to be true
+    post_types = [:posts, :other_posts, :special_posts]
+    # test both has_many and has_and_belongs_to_many
+    [Author, Category].each do |className|
+      d1 = find_all_ordered(className)
+      # test including all post types at once
+      d2 = find_all_ordered(className, post_types)
+      d1.each_index do |i|
+        assert_equal(d1[i], d2[i])
+        assert_equal_after_sort(d1[i].posts, d2[i].posts)
+        post_types[1..-1].each do |post_type|
+          # test including post_types together
+          d3 = find_all_ordered(className, [:posts, post_type])
+          assert_equal(d1[i], d3[i])
+          assert_equal_after_sort(d1[i].posts, d3[i].posts)
+          assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
+        end
+      end
+    end
+  end
+  
+  def test_eager_with_multiple_associations_with_same_table_has_one
+    d1 = find_all_ordered(Firm)
+    d2 = find_all_ordered(Firm, :account)
+    d1.each_index do |i|
+      assert_equal(d1[i], d2[i])
+      assert_equal(d1[i].account, d2[i].account)
+    end
+  end
+  
+  def test_eager_with_multiple_associations_with_same_table_belongs_to
+    firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
+    d1 = find_all_ordered(Client)
+    d2 = find_all_ordered(Client, firm_types)
+    d1.each_index do |i|
+      assert_equal(d1[i], d2[i])
+      firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
+    end
+  end
+  def test_eager_with_valid_association_as_string_not_symbol
+    assert_nothing_raised { Post.find(:all, :include =&gt; 'comments') }
+  end
+  
+  def test_preconfigured_includes_with_belongs_to
+    author = posts(:welcome).author_with_posts
+    assert_no_queries {assert_equal 5, author.posts.size}
+  end
+  
+  def test_preconfigured_includes_with_has_one
+    comment = posts(:sti_comments).very_special_comment_with_post
+    assert_no_queries {assert_equal posts(:sti_comments), comment.post}
+  end
+  
+  def test_preconfigured_includes_with_has_many
+    posts = authors(:david).posts_with_comments
+    one = posts.detect { |p| p.id == 1 }
+    assert_no_queries do
+      assert_equal 5, posts.size
+      assert_equal 2, one.comments.size
+    end
+  end
+  
+  def test_preconfigured_includes_with_habtm
+    posts = authors(:david).posts_with_categories
+    one = posts.detect { |p| p.id == 1 }
+    assert_no_queries do
+      assert_equal 5, posts.size
+      assert_equal 2, one.categories.size
+    end
+  end
+  
+  def test_preconfigured_includes_with_has_many_and_habtm
+    posts = authors(:david).posts_with_comments_and_categories
+    one = posts.detect { |p| p.id == 1 }
+    assert_no_queries do
+      assert_equal 5, posts.size
+      assert_equal 2, one.comments.size
+      assert_equal 2, one.categories.size
+    end
+  end
+  
+  def test_count_with_include
+    if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
+      assert_equal 3, authors(:david).posts_with_comments.count(:conditions =&gt; &quot;len(comments.body) &gt; 15&quot;)
+    elsif current_adapter?(:OpenBaseAdapter)
+      assert_equal 3, authors(:david).posts_with_comments.count(:conditions =&gt; &quot;length(FETCHBLOB(comments.body)) &gt; 15&quot;)
+    else
+      assert_equal 3, authors(:david).posts_with_comments.count(:conditions =&gt; &quot;length(comments.body) &gt; 15&quot;)
+    end
+  end
+  
+  def test_load_with_sti_sharing_association
+    assert_queries(2) do #should not do 1 query per subclass
+      Comment.find :all, :include =&gt; :post
+    end
+  end
+  
+  def test_conditions_on_join_table_with_include_and_limit
+    assert_equal 3, Developer.find(:all, :include =&gt; 'projects', :conditions =&gt; 'developers_projects.access_level = 1', :limit =&gt; 5).size
+  end
+  
+  def test_order_on_join_table_with_include_and_limit
+    assert_equal 5, Developer.find(:all, :include =&gt; 'projects', :order =&gt; 'developers_projects.joined_on DESC', :limit =&gt; 5).size
+  end
 end</diff>
      <filename>activerecord/test/cases/associations/eager_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>63cd122b9a81b5a1a44b09b088f855a9959ae86e</id>
    </parent>
  </parents>
  <author>
    <name>Zach Dennis</name>
    <email>zach.dennis@gmail.com</email>
  </author>
  <url>http://github.com/zdennis/rails/commit/ee5d1415533eed9c3f585580ca962c0965312e10</url>
  <id>ee5d1415533eed9c3f585580ca962c0965312e10</id>
  <committed-date>2008-06-12T20:23:13-07:00</committed-date>
  <authored-date>2008-06-12T20:02:41-07:00</authored-date>
  <message>made it backwards compatible with other eager loading things</message>
  <tree>0f604dc4eb7b9bc3b87296e9b4fd88aa1bd228c6</tree>
  <committer>
    <name>Zach Dennis</name>
    <email>zach.dennis@gmail.com</email>
  </committer>
</commit>
