<?xml version="1.0" encoding="UTF-8"?>
<ticket>
  <assigned-user-id type="integer" nil="true"></assigned-user-id>
  <attachments-count type="integer">3</attachments-count>
  <closed type="boolean">true</closed>
  <created-at type="datetime">2009-06-11T15:32:28+01:00</created-at>
  <creator-id type="integer">44567</creator-id>
  <milestone-due-on type="datetime" nil="true"></milestone-due-on>
  <milestone-id type="integer">9903</milestone-id>
  <number type="integer">2791</number>
  <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
  <priority type="integer">1892</priority>
  <project-id type="integer">8994</project-id>
  <raw-data type="binary" nil="true" encoding="base64"></raw-data>
  <state>duplicate</state>
  <tag>activerecord batches find_in_batches master</tag>
  <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
  <updated-at type="datetime">2009-09-11T11:10:28+01:00</updated-at>
  <user-id type="integer">7211</user-id>
  <user-name>Mike Gunderloy</user-name>
  <creator-name>Valentin Mihov</creator-name>
  <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
  <milestone-title>2.x</milestone-title>
  <original-body>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 &quot;hello&quot; 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.</original-body>
  <latest-body>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 &quot;hello&quot; 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.</latest-body>
  <original-body-html>&lt;div&gt;&lt;p&gt;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 &quot;hello&quot; in batches of 2 and
for each post get its author and count the number of posts he/she
has.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;</original-body-html>
  <versions type="array">
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">0</attachments-count>
      <body>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 &quot;hello&quot; 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.</body>
      <body-html>&lt;div&gt;&lt;p&gt;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 &quot;hello&quot; in batches of 2 and
for each post get its author and count the number of posts he/she
has.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-06-11T15:32:30+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-06-11T15:32:30+01:00</updated-at>
      <user-id type="integer">44567</user-id>
      <user-name>Valentin Mihov</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Here is the fix with adding with_exclusive_scope around the yield statement</body>
      <body-html>&lt;div&gt;&lt;p&gt;Here is the fix with adding with_exclusive_scope around the
yield statement&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-06-11T15:34:49+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-06-11T15:34:49+01:00</updated-at>
      <user-id type="integer">44567</user-id>
      <user-name>Valentin Mihov</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>Here is another fix of the problem. This time the with_scope is removed from the yield and no with_exclusive_scope is used. This one should be a better solution. There is some code duplication but I didn't figure out a way to remove it. Probably a private method could be made to avoid that?</body>
      <body-html>&lt;div&gt;&lt;p&gt;Here is another fix of the problem. This time the with_scope is
removed from the yield and no with_exclusive_scope is used. This
one should be a better solution. There is some code duplication but
I didn't figure out a way to remove it. Probably a private method
could be made to avoid that?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-06-11T17:15:29+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-06-11T17:15:29+01:00</updated-at>
      <user-id type="integer">44567</user-id>
      <user-name>Valentin Mihov</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>I don't understand why the exclusive scope is needed. Shouldn't you be able to do Shirt.red.find_each and find_each then do the right thing?</body>
      <body-html>&lt;div&gt;&lt;p&gt;I don't understand why the exclusive scope is needed. Shouldn't
you be able to do Shirt.red.find_each and find_each then do the
right thing?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-01T21:21:44+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-08-01T21:21:48+01:00</updated-at>
      <user-id type="integer">34034</user-id>
      <user-name>Elliot Winkler</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>The problem here is that when you make additional queries into a find_each block, the queries are affected in an unexpected behavior. 

Have a look at the unit test in the diff. In that example you have posts. Each post have a body and an author_id. You make a find_each for all posts with a given body (ex. &quot;Hello world!&quot;). In the find_each block imagine that you start processing a post from the author X. You want to count how many posts X has and you do Post.all(:conditions =&gt; { :author_id =&gt; id_of_author_X }). Unfortunately this query will return the number of posts from author X, which has a body equal to &quot;Hello world!&quot;, which is obviously not the idea in that situation.</body>
      <body-html>&lt;div&gt;&lt;p&gt;The problem here is that when you make additional queries into a
find_each block, the queries are affected in an unexpected
behavior.&lt;/p&gt;
&lt;p&gt;Have a look at the unit test in the diff. In that example you
have posts. Each post have a body and an author_id. You make a
find_each for all posts with a given body (ex. &quot;Hello world!&quot;). In
the find_each block imagine that you start processing a post from
the author X. You want to count how many posts X has and you do
Post.all(:conditions =&amp;gt; { :author_id =&amp;gt; id_of_author_X }).
Unfortunately this query will return the number of posts from
author X, which has a body equal to &quot;Hello world!&quot;, which is
obviously not the idea in that situation.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-19T09:18:59+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-08-19T09:19:01+01:00</updated-at>
      <user-id type="integer">44567</user-id>
      <user-name>Valentin Mihov</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>Ah, my bad, you were already one step ahead. +1, then -- makes sense to me.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Ah, my bad, you were already one step ahead. +1, then -- makes
sense to me.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-19T11:13:30+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-08-19T11:13:36+01:00</updated-at>
      <user-id type="integer">34034</user-id>
      <user-name>Elliot Winkler</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>There is a similar one at #2227</body>
      <body-html>&lt;div&gt;&lt;p&gt;There is a similar one at &lt;a href=&quot;/projects/8994/tickets/2227&quot;
title=&quot;Ticket #2227&quot;&gt;#2227&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-09-08T12:09:09+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-09-08T12:09:13+01:00</updated-at>
      <user-id type="integer">69039</user-id>
      <user-name>Thong Kuah</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>Yep... #2227 seems to be the same bug</body>
      <body-html>&lt;div&gt;&lt;p&gt;Yep... &lt;a href=&quot;/projects/8994/tickets/2227&quot; title=
&quot;Ticket #2227&quot;&gt;#2227&lt;/a&gt; seems to be the same bug&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-09-08T12:35:23+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-09-08T12:35:25+01:00</updated-at>
      <user-id type="integer">44567</user-id>
      <user-name>Valentin Mihov</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>&gt; Yep... #2227 seems to be the same bug

would you be willing to merge this ticket into #2227 and review the patch there? We are only one short of getting patch verified and applied. Thanks</body>
      <body-html>&lt;div&gt;&lt;blockquote&gt;
&lt;p&gt;Yep... &lt;a href=&quot;/projects/8994/tickets/2227&quot; title=
&quot;Ticket #2227&quot;&gt;#2227&lt;/a&gt; seems to be the same bug&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;would you be willing to merge this ticket into &lt;a href=
&quot;/projects/8994/tickets/2227&quot; title=&quot;Ticket #2227&quot;&gt;#2227&lt;/a&gt; and
review the patch there? We are only one short of getting patch
verified and applied. Thanks&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-09-10T13:33:10+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-09-10T13:33:13+01:00</updated-at>
      <user-id type="integer">69039</user-id>
      <user-name>Thong Kuah</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>Dupe of #2227</body>
      <body-html>&lt;div&gt;&lt;p&gt;Dupe of &lt;a href=&quot;/projects/8994/tickets/2227&quot; title=
&quot;Ticket #2227&quot;&gt;#2227&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2009-09-11T11:10:17+01:00</created-at>
      <creator-id type="integer">44567</creator-id>
      <diffable-attributes type="yaml">--- 
:state: new
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2791</number>
      <permalink>activerecordbasefind_in_batches-puts-a-with_scope-into-the-block-that-is-executed</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>duplicate</state>
      <tag>activerecord batches find_in_batches master</tag>
      <title>ActiveRecord::Base.find_in_batches puts a with_scope into the block that is executed</title>
      <updated-at type="datetime">2009-09-11T11:10:28+01:00</updated-at>
      <user-id type="integer">7211</user-id>
      <user-name>Mike Gunderloy</user-name>
      <creator-name>Valentin Mihov</creator-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2791</url>
      <milestone-title>2.x</milestone-title>
    </version>
  </versions>
  <attachments type="array">
    <attachment type="Attachment">
      <code>8aa3b39a98443e4f2839114c1ef39ebfaa38e8c5</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-06-11T15:32:29+01:00</created-at>
      <filename>0001-Added-a-unit-test-for-catching-the-with_scope-into-t.patch</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">132112</id>
      <size type="integer">1332</size>
      <uploader-id type="integer">44567</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/132112/0001-Added-a-unit-test-for-catching-the-with_scope-into-t.patch</url>
    </attachment>
    <attachment type="Attachment">
      <code>4a45a1a0e7ebbe76fdf9e3b313e35051cc07fbc5</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-06-11T15:34:47+01:00</created-at>
      <filename>0002-Added-with_exclusive_scope-around-the-find_in_batche.patch</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">132116</id>
      <size type="integer">1019</size>
      <uploader-id type="integer">44567</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/132116/0002-Added-with_exclusive_scope-around-the-find_in_batche.patch</url>
    </attachment>
    <attachment type="Attachment">
      <code>a4dcd5163456bdc9b6602c00a67c7585c359d4b1</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-06-11T17:15:25+01:00</created-at>
      <filename>removed_with_scope_around_yield.patch</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">132176</id>
      <size type="integer">3977</size>
      <uploader-id type="integer">44567</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/132176/removed_with_scope_around_yield.patch</url>
    </attachment>
  </attachments>
</ticket>
