Rails 5.1 adds support for limit in batch processing

Mohit Natoo

By Mohit Natoo

on May 23, 2017

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.

1
2 User.find_each{ |user| puts user.id }
3

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.

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

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.

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

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.

1
2 total_count = 0
3
4 User.limit(10).find_in_batches(batch_size: 4) do |batch|
5   total_count += batch.count
6 end
7
8 total_count
9#=> 10
10

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.