Merge pull request #1807 from caius/find_in_batches_id_bug

Bugfix by stopping find_in_batches using the records after yielding.
This commit is contained in:
Santiago Pastorino
2011-07-14 07:57:04 -07:00
2 changed files with 21 additions and 2 deletions

View File

@@ -66,11 +66,14 @@ module ActiveRecord
records = relation.where(table[primary_key].gteq(start)).all
while records.any?
records_size = records.size
primary_key_offset = records.last.id
yield records
break if records.size < batch_size
break if records_size < batch_size
if primary_key_offset = records.last.id
if primary_key_offset
records = relation.where(table[primary_key].gt(primary_key_offset)).to_a
else
raise "Primary key not included in the custom select clause"

View File

@@ -100,4 +100,20 @@ class EachTest < ActiveRecord::TestCase
end
end
end
def test_find_in_batches_should_not_use_records_after_yielding_them_in_case_original_array_is_modified
not_a_post = "not a post"
not_a_post.stubs(:id).raises(StandardError, "not_a_post had #id called on it")
assert_nothing_raised do
Post.find_in_batches(:batch_size => 1) do |batch|
assert_kind_of Array, batch
assert_kind_of Post, batch.first
batch.map! { not_a_post }
end
end
end
end