-
-
Save mileszs/392366 to your computer and use it in GitHub Desktop.
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
# Warning: Loses any scope it may have been called on! | |
def send_all_in_batches(batch_size, method, *args) | |
transaction do | |
start_id = first(:order => 'id ASC').id | |
end_id = first(:order => 'id DESC').id | |
current_id = start_id | |
while current_id <= end_id | |
if time = args.delete(:time) | |
send_at(time, :send_to_batch, current_id, current_id + batch_size, method, *args) | |
else | |
send_later :send_to_batch, current_id, current_id + batch_size, method, *args | |
end | |
current_id += batch_size | |
end | |
end | |
end | |
# Warning: Loses any scope it may have been called on! | |
def send_to_batch(start_id, end_id, method, *args) | |
all(:conditions => ['id >= ? AND id < ?', start_id, end_id]).each do |object| | |
object.send(method, *args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment