Skip to content

Instantly share code, notes, and snippets.

@marclerodrigues
Created April 3, 2020 15:08
Show Gist options
  • Select an option

  • Save marclerodrigues/a16b7074eab01d6a22ae06e6db8b4f39 to your computer and use it in GitHub Desktop.

Select an option

Save marclerodrigues/a16b7074eab01d6a22ae06e6db8b4f39 to your computer and use it in GitHub Desktop.
find_each inner works.
# 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