aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorVipul A M <vipulnsward@gmail.com>2013-09-17 11:54:58 +0530
committerVipul A M <vipulnsward@gmail.com>2015-02-09 01:33:57 +0530
commit3dc432068b295504be938e7d4d67bc628edbf850 (patch)
tree2aa318996facf953d0605f92d7a73bc97e1a3e69 /guides
parentde9a3748c436f849dd1877851115cd94663c2725 (diff)
downloadrails-3dc432068b295504be938e7d4d67bc628edbf850.tar.gz
rails-3dc432068b295504be938e7d4d67bc628edbf850.tar.bz2
rails-3dc432068b295504be938e7d4d67bc628edbf850.zip
Add an option `end_at` to `find_in_batches`
that complements the `start`parameter to specify where to stop batch processing
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_querying.md15
1 files changed, 14 insertions, 1 deletions
diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md
index 373a98bb85..c5ca848753 100644
--- a/guides/source/active_record_querying.md
+++ b/guides/source/active_record_querying.md
@@ -343,6 +343,19 @@ end
Another example would be if you wanted multiple workers handling the same processing queue. You could have each worker handle 10000 records by setting the appropriate `:start` option on each worker.
+**`:end_at`**
+
+Similar to the `:start` option, `:end_at` allows you to configure the last ID of the sequence whenever the highest ID is not the one you need.
+This would be useful, for example, if you wanted to run a batch process, using a subset of records based on `:start` and `:end_at`
+
+For example, to send newsletters only to users with the primary key starting from 2000 upto 10000 and to retrieve them in batches of 1000:
+
+```ruby
+User.find_each(start: 2000, end_at: 10000, batch_size: 5000) do |user|
+ NewsMailer.weekly(user).deliver_now
+end
+```
+
#### `find_in_batches`
The `find_in_batches` method is similar to `find_each`, since both retrieve batches of records. The difference is that `find_in_batches` yields _batches_ to the block as an array of models, instead of individually. The following example will yield to the supplied block an array of up to 1000 invoices at a time, with the final block containing any remaining invoices:
@@ -356,7 +369,7 @@ end
##### Options for `find_in_batches`
-The `find_in_batches` method accepts the same `:batch_size` and `:start` options as `find_each`.
+The `find_in_batches` method accepts the same `:batch_size`, `:start` and `:end_at` options as `find_each`.
Conditions
----------