Permalink
Browse files

Implement find_in_batches without with_scope [#2227 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent b8b568e commit 18ba648e0de032c6cb68cb48c2aaf3a347294e3d @libc libc committed with lifo Jul 2, 2009
Showing with 14 additions and 9 deletions.
  1. +8 −9 activerecord/lib/active_record/batches.rb
  2. +6 −0 activerecord/test/cases/batches_test.rb
View
17 activerecord/lib/active_record/batches.rb
@@ -59,19 +59,18 @@ def find_in_batches(options = {})
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"
View
6 activerecord/test/cases/batches_test.rb
@@ -58,4 +58,10 @@ def test_find_in_batches_shouldnt_excute_query_unless_needed
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

0 comments on commit 18ba648

Please sign in to comment.