Skip to content
This repository
Browse code

Fix #5667. Preloading should ignore scoping.

  • Loading branch information...
commit 86aefdb4f2b957acc6d6c7eef557dff6a3888803 1 parent 1141f71
Jon Leighton authored March 30, 2012
5  activerecord/lib/active_record/associations/preloader/association.rb
@@ -77,7 +77,7 @@ def associated_records_by_owner
77 77
             # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
78 78
             # Make several smaller queries if necessary or make one query if the adapter supports it
79 79
             sliced  = owner_keys.each_slice(model.connection.in_clause_length || owner_keys.size)
80  
-            records = sliced.map { |slice| records_for(slice) }.flatten
  80
+            records = sliced.map { |slice| records_for(slice).to_a }.flatten
81 81
           end
82 82
 
83 83
           # Each record may have multiple owners, and vice-versa
@@ -93,7 +93,8 @@ def associated_records_by_owner
93 93
         end
94 94
 
95 95
         def build_scope
96  
-          scope = klass.scoped
  96
+          scope = klass.unscoped
  97
+          scope.default_scoped = true
97 98
 
98 99
           scope = scope.where(interpolate(options[:conditions]))
99 100
           scope = scope.where(interpolate(preload_options[:conditions]))
11  activerecord/test/cases/associations/eager_test.rb
@@ -1169,4 +1169,15 @@ def test_deep_including_through_habtm
1169 1169
     assert_no_queries { assert_equal 1, posts[0].categories[1].categorizations.length }
1170 1170
     assert_no_queries { assert_equal 2, posts[1].categories[0].categorizations.length }
1171 1171
   end
  1172
+
  1173
+  test "scoping with a circular preload" do
  1174
+    assert_equal Comment.find(1), Comment.preload(:post => :comments).scoping { Comment.find(1) }
  1175
+  end
  1176
+
  1177
+  test "preload ignores the scoping" do
  1178
+    assert_equal(
  1179
+      Comment.find(1).post,
  1180
+      Post.where('1 = 0').scoping { Comment.preload(:post).find(1).post }
  1181
+    )
  1182
+  end
1172 1183
 end

0 notes on commit 86aefdb

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