This project is archived and is in readonly mode.

#2791 ✓duplicate
Valentin Mihov

ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed

Reported by Valentin Mihov | June 11th, 2009 @ 03:32 PM | in 2.x

When you use find_in_batches it turns out that there is a with_scope that gets executed, which could lead to unexpected behavior. See the attached unit test for an example. The idea of the test is to get all posts with body "hello" in batches of 2 and for each post get its author and count the number of posts he/she has.

The easiest fix is to put with_exclusive_scope around the yield, but that could affect people who used with_scope around find_in_batches. The harder fix will be not to use with_scope.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages