diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-10 17:16:48 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-10 17:16:48 -0200 |
commit | 5d180fdf06b4a051234f8876fc829557366b721b (patch) | |
tree | d4ee1f5dd387649cd4a7f51978659122187fe798 /guides | |
parent | 84c20e27b6ecf48b17b707403175a44a528c57d6 (diff) | |
parent | 3dc432068b295504be938e7d4d67bc628edbf850 (diff) | |
download | rails-5d180fdf06b4a051234f8876fc829557366b721b.tar.gz rails-5d180fdf06b4a051234f8876fc829557366b721b.tar.bz2 rails-5d180fdf06b4a051234f8876fc829557366b721b.zip |
Merge pull request #12257 from vipulnsward/end_on_find_in_batches
Add an option `end` to `find_in_batches`
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_querying.md | 15 |
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 ---------- |