Last Updated: June 16, 2016
·
171
· alameenkhader

Rails 5 - Whats new/changed

Rails 5 adds finish option in find_in_batches

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.

Reference: http://blog.bigbinary.com/2016/06/06/rails-5-provides-finish-option-for-find-in-batches.html