ActiveRecord::Batches module provides methods like find_each
, find_in_batches
,
and in_batches
to process records in batches, reducing memory consumption.
By default, records are processed in ascending order by primary key(ID).
Before
Before Rails 7.1, ActiveRecord::Batches
methods - like find_each
, find_in_batches
,
and in_batches
supported ORDER BY
the primary key (ID) in either ascending or descending order.
We could only specify a single sorting direction (:asc or :desc) as it only supported ORDER BY
on single primary column.
This restriction made it impossible to sort by multiple columns, such as having one column in ascending order while sorting another column in descending order, as ActiveRecord::Batches
did not support multi-column ordering.
After
Rails 7.1 adds support for batching using composite primary keys and multiple column ordering.
It allows selection of ascending or descending order for each key in composite primary key.
By default the order is set to ASC
. Valid values for the ORDER BY
clause are :asc
or :desc
or an array consisting of :asc
or :desc
and it’s case sensitive.
Passing anything other than :asc
, :desc
raises ArgumentError