<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1731,6 +1731,11 @@ module ActiveRecord
           return sanitize_sql(sql)
         end
 
+        def tables_in_string(string)
+          return [] if string.blank?
+          string.scan(/([\.a-zA-Z_]+).?\./).flatten
+        end
+
         def conditions_tables(options)
           # look in both sets of conditions
           conditions = [scope(:find, :conditions), options[:conditions]].inject([]) do |all, cond|
@@ -1741,37 +1746,55 @@ module ActiveRecord
               else            all &lt;&lt; cond
             end
           end
-          conditions.join(' ').scan(/([\.a-zA-Z_]+).?\./).flatten
+          tables_in_string(conditions.join(' '))
         end
 
         def order_tables(options)
           order = [options[:order], scope(:find, :order) ].join(&quot;, &quot;)
           return [] unless order &amp;&amp; order.is_a?(String)
-          order.scan(/([\.a-zA-Z_]+).?\./).flatten
+          tables_in_string(order)
         end
 
         def selects_tables(options)
           select = options[:select]
           return [] unless select &amp;&amp; select.is_a?(String)
-          select.scan(/&quot;?([\.a-zA-Z_]+)&quot;?.?\./).flatten
+          tables_in_string(select)
+        end
+
+        def joined_tables(options)
+          scope = scope(:find)
+          joins = options[:joins]
+          merged_joins = scope &amp;&amp; scope[:joins] &amp;&amp; joins ? merge_joins(scope[:joins], joins) : (joins || scope &amp;&amp; scope[:joins])
+          [table_name] + case merged_joins
+          when Symbol, Hash, Array
+            if array_of_strings?(merged_joins)
+              tables_in_string(merged_joins.join(' '))
+            else
+              join_dependency = ActiveRecord::Associations::ClassMethods::InnerJoinDependency.new(self, merged_joins, nil)
+              [table_name] + join_dependency.join_associations.collect {|join_association| [join_association.aliased_join_table_name, join_association.aliased_table_name]}.flatten.compact
+            end
+          else
+            tables_in_string(merged_joins)
+          end
         end
 
         # Checks if the conditions reference a table other than the current model table
-        def include_eager_conditions?(options, tables = nil)
-          ((tables || conditions_tables(options)) - [table_name]).any?
+        def include_eager_conditions?(options, tables = nil, joined_tables = nil)
+          ((tables || conditions_tables(options)) - (joined_tables || joined_tables(options))).any?
         end
 
         # Checks if the query order references a table other than the current model's table.
-        def include_eager_order?(options, tables = nil)
-          ((tables || order_tables(options)) - [table_name]).any?
+        def include_eager_order?(options, tables = nil, joined_tables = nil)
+          ((tables || order_tables(options)) - (joined_tables || joined_tables(options))).any?
         end
 
-        def include_eager_select?(options)
-          (selects_tables(options) - [table_name]).any?
+        def include_eager_select?(options, joined_tables = nil)
+          (selects_tables(options) - (joined_tables || joined_tables(options))).any?
         end
 
         def references_eager_loaded_tables?(options)
-          include_eager_order?(options) || include_eager_conditions?(options) || include_eager_select?(options)
+          joined_tables = joined_tables(options)
+          include_eager_order?(options, nil, joined_tables) || include_eager_conditions?(options, nil, joined_tables) || include_eager_select?(options, joined_tables)
         end
 
         def using_limitable_reflections?(reflections)</diff>
      <filename>activerecord/lib/active_record/associations.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,7 @@
 require &quot;cases/helper&quot;
 require 'models/post'
 require 'models/tagging'
+require 'models/tag'
 require 'models/comment'
 require 'models/author'
 require 'models/category'
@@ -706,4 +707,68 @@ class EagerAssociationTest &lt; ActiveRecord::TestCase
     assert_equal 5, Developer.find(:all, :include =&gt; 'projects', :order =&gt; 'developers_projects.joined_on DESC', :limit =&gt; 5).size
   end
 
+  def test_eager_loading_with_order_on_joined_table_preloads
+    posts = assert_queries(2) do
+      Post.find(:all, :joins =&gt; :comments, :include =&gt; :author, :order =&gt; 'comments.id DESC')
+    end
+    assert_equal posts(:eager_other), posts[0]
+    assert_equal authors(:mary), assert_no_queries { posts[0].author}
+  end
+
+  def test_eager_loading_with_conditions_on_joined_table_preloads
+    posts = assert_queries(2) do
+      Post.find(:all, :select =&gt; 'distinct posts.*', :include =&gt; :author, :joins =&gt; [:comments], :conditions =&gt; &quot;comments.body like 'Thank you%'&quot;, :order =&gt; 'posts.id')
+    end
+    assert_equal [posts(:welcome)], posts
+    assert_equal authors(:david), assert_no_queries { posts[0].author}
+
+    posts = assert_queries(2) do
+      Post.find(:all, :select =&gt; 'distinct posts.*', :include =&gt; :author, :joins =&gt; [:comments], :conditions =&gt; &quot;comments.body like 'Thank you%'&quot;, :order =&gt; 'posts.id')
+    end
+    assert_equal [posts(:welcome)], posts
+    assert_equal authors(:david), assert_no_queries { posts[0].author}
+
+    posts = assert_queries(2) do
+      Post.find(:all, :include =&gt; :author, :joins =&gt; {:taggings =&gt; :tag}, :conditions =&gt; &quot;tags.name = 'General'&quot;)
+    end
+    assert_equal posts(:welcome, :thinking), posts
+
+    posts = assert_queries(2) do
+      Post.find(:all, :include =&gt; :author, :joins =&gt; {:taggings =&gt; {:tag =&gt; :taggings}}, :conditions =&gt; &quot;taggings_tags.super_tag_id=2&quot;)
+    end
+    assert_equal posts(:welcome, :thinking), posts
+
+  end
+
+  def test_eager_loading_with_conditions_on_string_joined_table_preloads
+    posts = assert_queries(2) do
+      Post.find(:all, :select =&gt; 'distinct posts.*', :include =&gt; :author, :joins =&gt; &quot;INNER JOIN comments on comments.post_id = posts.id&quot;, :conditions =&gt; &quot;comments.body like 'Thank you%'&quot;, :order =&gt; 'posts.id')
+    end
+    assert_equal [posts(:welcome)], posts
+    assert_equal authors(:david), assert_no_queries { posts[0].author}
+
+    posts = assert_queries(2) do
+      Post.find(:all, :select =&gt; 'distinct posts.*', :include =&gt; :author, :joins =&gt; [&quot;INNER JOIN comments on comments.post_id = posts.id&quot;], :conditions =&gt; &quot;comments.body like 'Thank you%'&quot;, :order =&gt; 'posts.id')
+    end
+    assert_equal [posts(:welcome)], posts
+    assert_equal authors(:david), assert_no_queries { posts[0].author}
+
+  end
+
+  def test_eager_loading_with_select_on_joined_table_preloads
+    posts = assert_queries(2) do
+      Post.find(:all, :select =&gt; 'posts.*, authors.name as author_name', :include =&gt; :comments, :joins =&gt; :author, :order =&gt; 'posts.id')
+    end
+    assert_equal 'David', posts[0].author_name
+    assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
+  end
+
+  def test_eager_loading_with_conditions_on_join_model_preloads
+    authors = assert_queries(2) do
+      Author.find(:all, :include =&gt; :author_address, :joins =&gt; :comments, :conditions =&gt; &quot;posts.title like 'Welcome%'&quot;)
+    end
+    assert_equal authors(:david), authors[0]
+    assert_equal author_addresses(:david_address), authors[0].author_address
+  end
+
 end</diff>
      <filename>activerecord/test/cases/associations/eager_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>9cf6b1b15e6335134a5af5d9d6296f8d9242e005</id>
    </parent>
  </parents>
  <author>
    <name>Frederick Cheung</name>
    <email>frederick.cheung@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/c9ab7098be7bdd748c0f4a49c8ef015b4aad3108</url>
  <id>c9ab7098be7bdd748c0f4a49c8ef015b4aad3108</id>
  <committed-date>2008-12-18T11:19:36-08:00</committed-date>
  <authored-date>2008-12-18T11:07:55-08:00</authored-date>
  <message>Ensure :include checks joins when determining if it can preload [#528 state:resolved]</message>
  <tree>aedc907d37039a5cbf8dca55f6f7a6f84a37c0c3</tree>
  <committer>
    <name>Pratik Naik</name>
    <email>pratiknaik@gmail.com</email>
  </committer>
</commit>
