From 4867ad7674e9b030d5df1f9b102a105cae9992bd Mon Sep 17 00:00:00 2001 From: Eugene Pimenov Date: Thu, 2 Jul 2009 06:06:47 +0400 Subject: [PATCH] find_in_batches shouldn't clog conditions for find calls inside the block --- activerecord/lib/active_record/batches.rb | 17 ++++++++--------- activerecord/test/cases/batches_test.rb | 6 ++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/activerecord/lib/active_record/batches.rb b/activerecord/lib/active_record/batches.rb index e41d38f..55eb55e 100644 --- a/activerecord/lib/active_record/batches.rb +++ b/activerecord/lib/active_record/batches.rb @@ -57,19 +57,18 @@ module ActiveRecord start = options.delete(:start).to_i batch_size = options.delete(:batch_size) || 1000 - with_scope(:find => options.merge(:order => batch_order, :limit => batch_size)) do - records = find(:all, :conditions => [ "#{table_name}.#{primary_key} >= ?", start ]) + proxy = scoped(options.merge(:order => batch_order, :limit => batch_size)) + records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} >= ?", start ]) - while records.any? - yield records + while records.any? + yield records - break if records.size < batch_size - records = find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", records.last.id ]) - end + break if records.size < batch_size + records = proxy.find(:all, :conditions => [ "#{table_name}.#{primary_key} > ?", records.last.id ]) end end - - + + private def batch_order "#{table_name}.#{primary_key} ASC" diff --git a/activerecord/test/cases/batches_test.rb b/activerecord/test/cases/batches_test.rb index 5009a90..68b9ffd 100644 --- a/activerecord/test/cases/batches_test.rb +++ b/activerecord/test/cases/batches_test.rb @@ -58,4 +58,10 @@ class EachTest < ActiveRecord::TestCase Post.find_in_batches(:batch_size => post_count + 1) {|batch| assert_kind_of Array, batch } end end + + def test_find_in_batches_doesnt_clog_conditions + Post.find_in_batches(:conditions => {:id => posts(:welcome).id}) do + assert_nothing_raised { Post.find(posts(:thinking).id) } + end + end end \ No newline at end of file -- 1.6.3.2.198.g6096d