aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-02-05 20:10:45 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-02-05 20:10:45 -0200
commit84fe7b7db64352c0186a9d2a51f4eb32d2825d2a (patch)
tree49ab417231c91102b3b878aaaf10285f1a465785 /activerecord/lib
parent1df4dcf7dd9ec5cf8fed3c04d9427ad925f2b83b (diff)
parenta476020567a47f5fbec3629707d5bf31b400a284 (diff)
downloadrails-84fe7b7db64352c0186a9d2a51f4eb32d2825d2a.tar.gz
rails-84fe7b7db64352c0186a9d2a51f4eb32d2825d2a.tar.bz2
rails-84fe7b7db64352c0186a9d2a51f4eb32d2825d2a.zip
Merge pull request #13938 from marcandre/sized_enumerator
Sized enumerator
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/relation/batches.rb17
-rw-r--r--activerecord/lib/active_record/result.rb2
2 files changed, 13 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb
index 666cef80a9..29fc150b3d 100644
--- a/activerecord/lib/active_record/relation/batches.rb
+++ b/activerecord/lib/active_record/relation/batches.rb
@@ -52,7 +52,9 @@ module ActiveRecord
records.each { |record| yield record }
end
else
- enum_for :find_each, options
+ enum_for :find_each, options do
+ options[:start] ? where(table[primary_key].gteq(options[:start])).size : size
+ end
end
end
@@ -96,17 +98,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
diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb
index 469451e2f4..228b2aa60f 100644
--- a/activerecord/lib/active_record/result.rb
+++ b/activerecord/lib/active_record/result.rb
@@ -54,7 +54,7 @@ module ActiveRecord
if block_given?
hash_rows.each { |row| yield row }
else
- hash_rows.to_enum
+ hash_rows.to_enum { @rows.size }
end
end