public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added logic to associations.rb to make sure select_for_limited_ids

includes joins that are needed to reach tables listed in the :order
or :conditions options if they are not joined directly to the main
active_record table.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#109 state:resolved]
jdevine (author)
Sat May 03 20:49:18 -0700 2008
NZKoz (committer)
Tue May 06 02:47:10 -0700 2008
commit  8ded457b1b31b157d6fe89b553749579e5ac4a27
tree    9c9c4689ad1f9efe952b737bbf45eb9455237901
parent  fbebdb0c091c37b0bc75ab774d187d8bc8795bd2
...
1446
1447
1448
 
 
 
 
 
 
1449
1450
1451
...
1457
1458
1459
1460
 
1461
1462
1463
...
1617
1618
1619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1620
1621
1622
...
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
...
1463
1464
1465
 
1466
1467
1468
1469
...
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
0
@@ -1446,6 +1446,12 @@ module ActiveRecord
0
           tables_from_conditions = conditions_tables(options)
0
           tables_from_order      = order_tables(options)
0
           all_tables             = tables_from_conditions + tables_from_order
0
+          distinct_join_associations = all_tables.uniq.map{|table|
0
+            join_dependency.joins_for_table_name(table)
0
+          }.flatten.compact.uniq
0
+
0
+
0
+
0
 
0
           is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order)
0
           sql = "SELECT "
0
@@ -1457,7 +1463,7 @@ module ActiveRecord
0
           sql << " FROM #{connection.quote_table_name table_name} "
0
 
0
           if is_distinct
0
-            sql << join_dependency.join_associations.reject{ |ja| !all_tables.include?(ja.table_name) }.collect(&:association_join).join
0
+            sql << distinct_join_associations.collect(&:association_join).join
0
             add_joins!(sql, options, scope)
0
           end
0
 
0
@@ -1617,6 +1623,23 @@ module ActiveRecord
0
             end
0
           end
0
 
0
+          def join_for_table_name(table_name)
0
+            @joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil
0
+          end
0
+
0
+          def joins_for_table_name(table_name)
0
+            join = join_for_table_name(table_name)
0
+            result = nil
0
+            if join && join.is_a?(JoinAssociation)
0
+              result = [join]
0
+              if join.parent && join.parent.is_a?(JoinAssociation)
0
+                result = joins_for_table_name(join.parent.aliased_table_name) +
0
+                         result
0
+              end
0
+            end
0
+            result
0
+          end
0
+
0
           protected
0
             def build(associations, parent = nil)
0
               parent ||= @joins.last
...
8
9
10
 
11
12
13
...
857
858
859
 
 
 
 
 
 
 
 
860
861
862
...
8
9
10
11
12
13
14
...
858
859
860
861
862
863
864
865
866
867
868
869
870
871
0
@@ -8,6 +8,7 @@ require 'models/entrant'
0
 require 'models/developer'
0
 require 'models/post'
0
 require 'models/customer'
0
+require 'models/job'
0
 
0
 class FinderTest < ActiveRecord::TestCase
0
   fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers
0
@@ -857,6 +858,14 @@ class FinderTest < ActiveRecord::TestCase
0
       Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}}
0
   end
0
 
0
+  def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
0
+    assert_equal 2, Post.find(:all,:include=>{:authors=>:author_address},:order=>' author_addresses.id DESC ', :limit=>2).size
0
+
0
+    assert_equal 3, Post.find(:all,:include=>{:author=>:author_address,:authors=>:author_address},
0
+                              :order=>' author_addresses_authors.id DESC ', :limit=>3).size
0
+  end
0
+
0
+
0
   protected
0
     def bind(statement, *vars)
0
       if vars.first.is_a?(Hash)

Comments