Created
April 3, 2020 15:08
-
-
Save marclerodrigues/a16b7074eab01d6a22ae06e6db8b4f39 to your computer and use it in GitHub Desktop.
find_each inner works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # find_each loses order of previous sorted relations | |
| # under the hook it users some primary key ordering | |
| # a workaround to perform iterations in batches is | |
| batch_size = 100 | |
| ordered_ids = Model.ordered.pluck(:id) | |
| ordered_ids.each_slice(batch_size) do |slice| | |
| HeavyWork.call(slice) | |
| end | |
| # in cases where you don't need to process in batches you can go with | |
| # the normal each method | |
| records = Model.ordered | |
| records.each { "..." } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment