I need to mass-update many thousands of records, and I would like to process the updates in batches. First, I tried:
Foo.where(bar: \'bar\').find_in_batches.upda
I'm surprised, too, that there isn't an easier way to do this... but I did come up with this approach:
batch_size = 1000
0.step(Foo.count, batch_size).each do |offset|
Foo.where(bar: 'bar').order(:id)
.offset(offset)
.limit(batch_size)
.update_all(bar: 'baz')
end
Basically this will:
0 and Foo.count stepping by batch_size each time. For example, if Foo.count == 10500 you'd get: [0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000]id, and limiting to the batch_size.batch_size records whose "index" is greater than offset.This is basically the manual way to perform what you said you were hoping for in the generated SQL. Too bad it can't just be done this way already by a standard library method... though I'm sure you could create one of your own.