Back to Blog

Rails 5 adds finish option in find_in_batches

on June 6, 2016
This blog is part of our Rails 5 series.

In Rails 4.x we had start option in find_in_batches method.

1
2Person.find_in_batches(start: 1000, batch_size: 2000) do |group|
3  group.each { |person| person.party_all_night! }
4end
5

The above code provides batches of Person starting from record whose value of primary key is equal to 1000.

There is no end value for primary key. That means in the above case all the records that have primary key value greater than 1000 are fetched.

Rails 5 introduces finish option that serves as an upper limit to the primary key value in the records being fetched.

1
2Person.find_in_batches(start: 1000, finish: 9500, batch_size: 2000) do |group|
3  group.each { |person| person.party_all_night! }
4end
5

The above code ensures that no record in any of the batches has the primary key value greater than 9500.


You might also like

If you liked this blog post, check out similar ones from BigBinary