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.
Person.find_in_batches(start: 1000, batch_size: 2000) do |group|
group.each { |person| person.party_all_night! }
end
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.
Person.find_in_batches(start: 1000, finish: 9500, batch_size: 2000) do |group|
group.each { |person| person.party_all_night! }
end
The above code ensures that no record in any of the batches has the primary key value greater than 9500.
If this blog was helpful, check out our full blog archive.