Skip to content

Instantly share code, notes, and snippets.

@mileszs
Forked from jqr/snippet.rb
Created May 6, 2010 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mileszs/392366 to your computer and use it in GitHub Desktop.
Save mileszs/392366 to your computer and use it in GitHub Desktop.
# 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