aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/batches.rb
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-06-25 20:09:17 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-06-25 20:09:17 -0300
commit840c552047a660d0a66883fb9c0cb144d5e728fb (patch)
tree1d7951a9163a3c7b9c80be2ef4a4eb3e53ec013a /activerecord/lib/active_record/relation/batches.rb
parent755069ee4e954ae66f6a8bb4a810c2dbb1bbc0d7 (diff)
parent25042359b388a73bae798e023276df59e53c74e2 (diff)
downloadrails-840c552047a660d0a66883fb9c0cb144d5e728fb.tar.gz
rails-840c552047a660d0a66883fb9c0cb144d5e728fb.tar.bz2
rails-840c552047a660d0a66883fb9c0cb144d5e728fb.zip
Merge pull request #10992 from Empact/find-each-enumerator
When .find_each is called without a block, return an Enumerator.
Diffstat (limited to 'activerecord/lib/active_record/relation/batches.rb')
-rw-r--r--activerecord/lib/active_record/relation/batches.rb15
1 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb
index 41291844fc..91ea1756c4 100644
--- a/activerecord/lib/active_record/relation/batches.rb
+++ b/activerecord/lib/active_record/relation/batches.rb
@@ -19,6 +19,13 @@ module ActiveRecord
# person.party_all_night!
# end
#
+ # If you do not provide a block to #find_each, it will return an Enumerator
+ # for chaining with other methods:
+ #
+ # Person.find_each.with_index do |person, index|
+ # person.award_trophy(index + 1)
+ # end
+ #
# ==== Options
# * <tt>:batch_size</tt> - Specifies the size of the batch. Default to 1000.
# * <tt>:start</tt> - Specifies the starting point for the batch processing.
@@ -40,8 +47,12 @@ module ActiveRecord
# NOTE: You can't set the limit either, that's used to control
# the batch sizes.
def find_each(options = {})
- find_in_batches(options) do |records|
- records.each { |record| yield record }
+ if block_given?
+ find_in_batches(options) do |records|
+ records.each { |record| yield record }
+ end
+ else
+ enum_for :find_each, options
end
end