# 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