Skip to content
This repository
Browse code

Fix find_in_batches against string IDs when start option is not speci…

…fied.

Conflicts:

	activerecord/CHANGELOG.md
	activerecord/lib/active_record/relation/batches.rb
  • Loading branch information...
commit 16d98b2a41dec3619b9bd48b6b534406d9d07ef4 1 parent 7031e36
Alexis Bernard authored
4 activerecord/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,9 @@
1 1 ## Rails 3.2.9 (unreleased)
2 2
  3 +* Fix `find_in_batches` crashing when IDs are strings and start option is not specified.
  4 +
  5 + *Alexis Bernard*
  6 +
3 7 * Fix issue with collection associations calling first(n)/last(n) and attempting
4 8 to set the inverse association when `:inverse_of` was used. Fixes #8087.
5 9
4 activerecord/lib/active_record/relation/batches.rb
@@ -59,11 +59,11 @@ def find_in_batches(options = {})
59 59 relation = apply_finder_options(finder_options)
60 60 end
61 61
62   - start = options.delete(:start) || 0
  62 + start = options.delete(:start)
63 63 batch_size = options.delete(:batch_size) || 1000
64 64
65 65 relation = relation.reorder(batch_order).limit(batch_size)
66   - records = relation.where(table[primary_key].gteq(start)).all
  66 + records = start ? relation.where(table[primary_key].gteq(start)).to_a : relation.to_a
67 67
68 68 while records.any?
69 69 records_size = records.size
9 activerecord/test/cases/batches_test.rb
@@ -148,4 +148,13 @@ def test_find_in_batches_should_use_any_column_as_primary_key
148 148
149 149 assert_equal nick_order_subscribers[1..-1].map(&:id), subscribers.map(&:id)
150 150 end
  151 +
  152 + def test_find_in_batches_should_use_any_column_as_primary_key_when_start_is_not_specified
  153 + Subscriber.count('nick') # preheat arel's table cache
  154 + assert_queries(Subscriber.count + 1) do
  155 + Subscriber.find_each(:batch_size => 1) do |subscriber|
  156 + assert_kind_of Subscriber, subscriber
  157 + end
  158 + end
  159 + end
151 160 end

0 comments on commit 16d98b2

Please sign in to comment.
Something went wrong with that request. Please try again.