Skip to content

Instantly share code, notes, and snippets.

@travisofthenorth
Created September 29, 2017 18:33
Show Gist options
  • Save travisofthenorth/602c09e012d19993ded33285a2c1fe30 to your computer and use it in GitHub Desktop.
Save travisofthenorth/602c09e012d19993ded33285a2c1fe30 to your computer and use it in GitHub Desktop.
Pluck in batches
module MorePluckable
extend ActiveSupport::Concern
class_methods do
def pluck_in_batches(*column_names, batch_size: 1000)
column_names = column_names.map(&:to_sym)
column_names.delete(:id)
column_names.unshift(:id)
data = []
start = 0
loop do
new_data = reorder(:id).where('id > ?', start).limit(batch_size).pluck(*column_names)
if block_given?
yield new_data
else
data.concat(new_data)
end
break if new_data.count < batch_size
start = new_data.last.first
end
data unless block_given?
end
end
end
ApplicationRecord.send(:include, MorePluckable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment