This blog is part of our Rails 5.1 series.

Before Rails 5.1, we were not able to limit the number of records fetched in batch processing.

Let’s take an example. Assume our system has 20 users.

 User.find_each{ |user| puts user.id }

The above code will print ids of all the 20 users.

There was no way to limit the number of records. Active Record’s limit method didn’t work for batches.

 User.limit(10).find_each{ |user| puts user.id }

The above code still prints ids of all 20 users, even though the intention was to limit the records fetched to 10.

Rails 5.1 has added support to limit the records in batch processing.

 User.limit(10).find_each{ |user| puts user.id }

The above code will print only 10 ids in Rails 5.1.

We can make use of limit in find_in_batches and in_batches as well.

 total_count = 0

 User.limit(10).find_in_batches(batch_size: 4) do |batch|
   total_count += batch.count
 end

 total_count
#=> 10