Loop using count + modulo (when applicable) in Ruby -


to break long migration of data, i'm using query limited groups of 100, processing 100 records.

something this...

count = model.where("conditions").count count = count / 100 count = count+1 if count%100 != 0 count.times   #do data migration steps .limit(100)... end 

is there shortcut or better way of doing count based on whether or not there remainder when dividing 100? feels i'm forgetting easy way (besides rounding seems slower, maybe it's not).

yes. supported rails, not have roll own code finding batches of records.

the easiest use find_each, seamlessly loads 1000 records @ time:

model.find_each |model|   # ... end 

the underlying mechanism find_in_batches default batch size of 1000. can use find_in_batches directly, not have to, find_each sufficient:

model.find_in_batches(batch_size: 100) |batch|   batch.each |model|     # ...   end end 

Popular posts from this blog