Skip to content
This repository
Browse code

Support eager includes when going through a polymorphic has_many asso…

…ciation. [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4171 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 58c435a3caebbe31c02e80c8f32840f0daa57d68 1 parent a9fb154
risk danger olson authored April 05, 2006
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Support eager includes when going through a polymorphic has_many association. [Rick]
  4
+
3 5
 * Added support for eagerly including polymorphic has_one associations. (closes #4525) [Rick]
4 6
 
5 7
     class Post < ActiveRecord::Base
50  activerecord/lib/active_record/associations.rb
@@ -1445,24 +1445,38 @@ def association_join
1445 1445
                           aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key
1446 1446
                         ]
1447 1447
                       else
1448  
-                        case source_reflection.macro
1449  
-                          when :belongs_to
1450  
-                            first_key  = primary_key
1451  
-                            second_key = options[:foreign_key] || klass.to_s.classify.foreign_key
1452  
-                          when :has_many
1453  
-                            first_key  = through_reflection.klass.to_s.classify.foreign_key
1454  
-                            second_key = options[:foreign_key] || primary_key
  1448
+                        if source_reflection.macro == :has_many && source_reflection.options[:as]
  1449
+                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s "  % [
  1450
+                            table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
  1451
+                            through_reflection.primary_key_name,
  1452
+                            parent.aliased_table_name, parent.primary_key] +
  1453
+                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
  1454
+                            table_name_and_alias,
  1455
+                            aliased_table_name, "#{source_reflection.options[:as]}_id", 
  1456
+                            aliased_join_table_name, options[:foreign_key] || primary_key,
  1457
+                            aliased_table_name, "#{source_reflection.options[:as]}_type", 
  1458
+                            klass.quote(source_reflection.active_record.base_class.name)
  1459
+                          ]
  1460
+                        else
  1461
+                          case source_reflection.macro
  1462
+                            when :belongs_to
  1463
+                              first_key  = primary_key
  1464
+                              second_key = options[:foreign_key] || klass.to_s.classify.foreign_key
  1465
+                            when :has_many
  1466
+                              first_key  = through_reflection.klass.to_s.classify.foreign_key
  1467
+                              second_key = options[:foreign_key] || primary_key
  1468
+                          end
  1469
+                          
  1470
+                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s "  % [
  1471
+                            table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
  1472
+                            through_reflection.primary_key_name,
  1473
+                            parent.aliased_table_name, parent.primary_key] +
  1474
+                          " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
  1475
+                            table_name_and_alias,
  1476
+                            aliased_table_name, first_key, 
  1477
+                            aliased_join_table_name, second_key
  1478
+                          ]
1455 1479
                         end
1456  
-
1457  
-                        " LEFT OUTER JOIN %s ON %s.%s = %s.%s "  % [
1458  
-                          table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name,
1459  
-                          through_reflection.primary_key_name,
1460  
-                          parent.aliased_table_name, parent.primary_key] +
1461  
-                        " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [
1462  
-                          table_name_and_alias,
1463  
-                          aliased_table_name, first_key, 
1464  
-                          aliased_join_table_name, second_key
1465  
-                        ]
1466 1480
                       end
1467 1481
                     
1468 1482
                     when reflection.macro == :has_many && reflection.options[:as]
@@ -1479,7 +1493,7 @@ def association_join
1479 1493
                         aliased_table_name, "#{reflection.options[:as]}_id",
1480 1494
                         parent.aliased_table_name, parent.primary_key,
1481 1495
                         aliased_table_name, "#{reflection.options[:as]}_type",
1482  
-                        klass.quote(reflection.active_record.name)
  1496
+                        klass.quote(reflection.active_record.base_class.name)
1483 1497
                       ]
1484 1498
                     else
1485 1499
                       foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
2  activerecord/test/associations_join_model_test.rb
@@ -311,7 +311,7 @@ def test_has_many_through_polymorphic_has_many
311 311
     assert_equal [taggings(:welcome_general), taggings(:thinking_general)], authors(:david).taggings.uniq.sort_by { |t| t.id }
312 312
   end
313 313
 
314  
-  def test_has_many_through_polymorphic_has_many_with_eager_loading
  314
+  def test_include_has_many_through_polymorphic_has_many
315 315
     author            = Author.find_by_id(authors(:david).id, :include => :taggings)
316 316
     expected_taggings = [taggings(:welcome_general), taggings(:thinking_general)]
317 317
     assert_no_queries do

0 notes on commit 58c435a

Please sign in to comment.
Something went wrong with that request. Please try again.