Rails provides two methods that address this problem by dividing records into memory-friendly batches for processing.The first method, methods are intended for use in the batch processing of a large number of records that wouldn't fit in memory all at once.If you just need to loop over a thousand records the regular find methods are the preferred option.
This would be useful, for example, if you wanted to resume an interrupted batch process, provided you saved the last processed ID as a checkpoint.For example, to send newsletters only to users with the primary key starting from 2000, and to retrieve them in batches of 5000: allows you to configure the last ID of the sequence whenever the highest ID is not the one you need.This would be useful, for example, if you wanted to run a batch process, using a subset of records based on to the block as an array of models, instead of individually.If you're used to using raw SQL to find database records, then you will generally find that there are better ways to carry out the same operations in Rails.Active Record insulates you from the need to use SQL in most cases.
Code examples throughout this guide will refer to one or more of the following models: Active Record will perform queries on the database for you and is compatible with most database systems, including My SQL, Maria DB, Postgre SQL and SQLite.
Regardless of which database system you're using, the Active Record method format will always be the same.
To retrieve objects from the database, Active Record provides several finder methods.
Each finder method allows you to pass arguments into it to perform certain queries on your database without writing raw SQL.
The methods are: in a single pass, build a model object per row, and then keep the entire array of model objects in memory.
Indeed, if we have a large number of records, the entire collection may exceed the amount of memory available.