0

To break up a long migration of data, I'm using a query limited to groups of 100, then processing those 100 records.

something like this...

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

is there a shortcut or better way of doing that count based on whether or not there is a remainder when dividing by 100? Feels like I'm forgetting an easy way (besides rounding which seems slower, but maybe it's not).

2 Answers 2

2

Yes. This is very well supported by Rails, you do not have to roll your own code for finding batches of records.

The easiest is to simply use find_each, which seamlessly loads 1000 records at a time:

Model.find_each do |model|
  # ...
end

The underlying mechanism is find_in_batches with a default batch size of 1000. You can use find_in_batches directly, but you do not have to, find_each is sufficient:

Model.find_in_batches(batch_size: 100) do |batch|
  batch.each do |model|
    # ...
  end
end
Sign up to request clarification or add additional context in comments.

1 Comment

I knew there was something I was forgetting about that made this much easier. Thank you for such a detailed answer.
1

Rails has several methods for loading records in batches. find_each would work nicely here. It defaults to batches of 1000, but you can specify the batch size:

Model.find_each(batch_size: 100) do |record|
  ...
end 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.