aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/batches.rb
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2014-01-29 14:53:54 -0500
committerMarc-Andre Lafortune <github@marc-andre.ca>2014-02-05 16:49:29 -0500
commitd37f395be86d131d0218dadd189771f99b06874f (patch)
tree2e7e4fd9e918142725a263bc61f611a787b28e38 /activerecord/lib/active_record/relation/batches.rb
parent4499ab5fdeca46416cfc4a50a376eba4eb8e16b4 (diff)
downloadrails-d37f395be86d131d0218dadd189771f99b06874f.tar.gz
rails-d37f395be86d131d0218dadd189771f99b06874f.tar.bz2
rails-d37f395be86d131d0218dadd189771f99b06874f.zip
Return sized enumerator from Batches#find_in_batches
Diffstat (limited to 'activerecord/lib/active_record/relation/batches.rb')
-rw-r--r--activerecord/lib/active_record/relation/batches.rb13
1 files changed, 9 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb
index 666cef80a9..a06da659cb 100644
--- a/activerecord/lib/active_record/relation/batches.rb
+++ b/activerecord/lib/active_record/relation/batches.rb
@@ -96,17 +96,22 @@ module ActiveRecord
# the batch sizes.
def find_in_batches(options = {})
options.assert_valid_keys(:start, :batch_size)
- return to_enum(:find_in_batches, options) unless block_given?
relation = self
+ start = options[:start]
+ batch_size = options[:batch_size] || 1000
+
+ unless block_given?
+ return to_enum(:find_in_batches, options) do
+ total = start ? where(table[primary_key].gteq(start)).size : size
+ (total - 1).div(batch_size) + 1
+ end
+ end
if logger && (arel.orders.present? || arel.taken.present?)
logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
end
- start = options[:start]
- batch_size = options[:batch_size] || 1000
-
relation = relation.reorder(batch_order).limit(batch_size)
records = start ? relation.where(table[primary_key].gteq(start)).to_a : relation.to_a