Created
September 29, 2017 18:33
-
-
Save travisofthenorth/602c09e012d19993ded33285a2c1fe30 to your computer and use it in GitHub Desktop.
Pluck in batches
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
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