Skip to content
This repository
Browse code

Fix tests under postgres - we should always put conditions in the WHE…

…RE part not in ON constraints because postgres requires that the table has been joined before the condition references it.
  • Loading branch information...
commit 37d93ea16046add35fecd8c279e868869ee744a5 1 parent 17ea204
Jon Leighton authored March 12, 2011
13  activerecord/lib/active_record/associations/association_scope.rb
@@ -86,11 +86,14 @@ def add_constraints(scope)
86 86
               scope = scope.where(interpolate(condition))
87 87
             end
88 88
           else
89  
-            scope = scope.joins(join(
90  
-              foreign_table,
91  
-              table[key].eq(foreign_table[foreign_key]),
92  
-              *conditions[i]
93  
-            ))
  89
+            constraint = table[key].eq(foreign_table[foreign_key])
  90
+            join       = join(foreign_table, constraint)
  91
+
  92
+            scope = scope.joins(join)
  93
+
  94
+            unless conditions[i].empty?
  95
+              scope = scope.where(sanitize(conditions[i], table))
  96
+            end
94 97
           end
95 98
         end
96 99
 
19  activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -74,7 +74,7 @@ def join_to(relation)
74 74
               foreign_key = reflection.foreign_key
75 75
             when :has_and_belongs_to_many
76 76
               # Join the join table first...
77  
-              relation = relation.from(join(
  77
+              relation.from(join(
78 78
                 table,
79 79
                 table[reflection.foreign_key].
80 80
                   eq(foreign_table[reflection.active_record_primary_key])
@@ -89,17 +89,20 @@ def join_to(relation)
89 89
               foreign_key = reflection.active_record_primary_key
90 90
             end
91 91
 
92  
-            conditions = self.conditions[i]
  92
+            constraint = table[key].eq(foreign_table[foreign_key])
93 93
 
94 94
             if reflection.klass.finder_needs_type_condition?
95  
-              conditions += [reflection.klass.send(:type_condition, table)]
  95
+              constraint = table.create_and([
  96
+                constraint,
  97
+                reflection.klass.send(:type_condition, table)
  98
+              ])
96 99
             end
97 100
 
98  
-            relation = relation.from(join(
99  
-              table,
100  
-              table[key].eq(foreign_table[foreign_key]),
101  
-              *conditions
102  
-            ))
  101
+            relation.from(join(table, constraint))
  102
+
  103
+            unless conditions[i].empty?
  104
+              relation.where(sanitize(conditions[i], table))
  105
+            end
103 106
 
104 107
             # The current table in this iteration becomes the foreign table in the next
105 108
             foreign_table = table
8  activerecord/lib/active_record/associations/join_helper.rb
@@ -38,13 +38,11 @@ def table_alias_for(reflection, join = false)
38 38
         name
39 39
       end
40 40
 
41  
-      def join(table, *conditions)
42  
-        table.create_join(table, table.create_on(sanitize(conditions)), join_type)
  41
+      def join(table, constraint)
  42
+        table.create_join(table, table.create_on(constraint), join_type)
43 43
       end
44 44
 
45  
-      def sanitize(conditions)
46  
-        table = conditions.first.left.relation
47  
-
  45
+      def sanitize(conditions, table)
48 46
         conditions = conditions.map do |condition|
49 47
           condition = active_record.send(:sanitize_sql, interpolate(condition), table.table_alias || table.name)
50 48
           condition = Arel.sql(condition) unless condition.is_a?(Arel::Node)
2  activerecord/test/cases/associations/nested_through_associations_test.rb
@@ -272,7 +272,7 @@ def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection
272 272
 
273 273
   def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins
274 274
     assert_includes_and_joins_equal(
275  
-      Author.where('comments.id' => comments(:does_it_hurt).id).order('comments.id'),
  275
+      Author.where('comments.id' => comments(:does_it_hurt).id).order('authors.id'),
276 276
       [authors(:david), authors(:mary)], :category_post_comments
277 277
     )
278 278
   end

0 notes on commit 37d93ea

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