Browse files

Merge pull request #8073 from alexisbernard/fix_find_in_batches_start…

…_option

Fix find_in_batches against string IDs when start option is not specified
  • Loading branch information...
2 parents 6a6909d + a58cafe commit 92bae4929dbc3ca65b44d8338b44789b5e320313 @carlosantoniodasilva carlosantoniodasilva committed Oct 31, 2012
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Fix `find_in_batches` crashing when IDs are strings and start option is not specified.
+
+ *Alexis Bernard*
+
* `AR::Base#attributes_before_type_cast` now returns unserialized values for serialized attributes.
*Nikita Afanasenko*
View
4 activerecord/lib/active_record/relation/batches.rb
@@ -62,11 +62,11 @@ def find_in_batches(options = {})
ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
end
- start = options.delete(:start) || 0
+ start = options.delete(:start)
batch_size = options.delete(:batch_size) || 1000
relation = relation.reorder(batch_order).limit(batch_size)
- records = relation.where(table[primary_key].gteq(start)).to_a
+ records = start ? relation.where(table[primary_key].gteq(start)).to_a : relation.to_a
while records.any?
records_size = records.size
View
9 activerecord/test/cases/batches_test.rb
@@ -136,4 +136,13 @@ def test_find_in_batches_should_use_any_column_as_primary_key
assert_equal nick_order_subscribers[1..-1].map(&:id), subscribers.map(&:id)
end
+
+ def test_find_in_batches_should_use_any_column_as_primary_key_when_start_is_not_specified
+ Subscriber.count('nick') # preheat arel's table cache
+ assert_queries(Subscriber.count + 1) do
+ Subscriber.find_each(:batch_size => 1) do |subscriber|
+ assert_kind_of Subscriber, subscriber
+ end
+ end
+ end
end

0 comments on commit 92bae49

Please sign in to comment.