Skip to content

Instantly share code, notes, and snippets.

@jqr
Created May 5, 2010 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jqr/391498 to your computer and use it in GitHub Desktop.
Save jqr/391498 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
send_later :send_to_batch, current_id, current_id + batch_size, method, *args)
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