Hi Friends,
Many times we get into a situation where we have to fetch a large record from a table and need to iterate over it and that makes our server cry. As it consumes a lots a memory and time. So no need to worry again for that Rails has a solution to retrieve records in batches, There are two methods given in Rails for retrieving records in batches:
(a)find_each
(b)find_in_batches
(a)find_each: It makes looping over a collection of Active record objects much cheaper by performing a batch retrieval of the database records and caching them. So the process runs in batches and you can specify batch sizes of your own.
Example:
Blog.find_each do |blog|
export_to_csv(blog.user.email)
end
It also allows all the methods of find except
order and
limit.Some optional methods are:
begin_at: It allows you to configure the first ID of the sequence.
batch_size: It specifies the number of records to be fetched in each batch.
end_at: It is similar to begin_at, but it specifies the last ID of the sequence.
An example containing all the options is:
Blog.find_each(begin_at: 100, end_at: 500, batch_size: 50) do |blog|
export_to_csv(blog.user.email)
end
(b)find_in_batches: It is similar to find_each, the difference is that it takes batches to the block as an array of models, in place of taking them individually.
Example:
#Export CSV of emails of bloggers 150 at each time
Blog.find_in_batches do |blogs|
fetch_emails_and_export_to_csv(blogs)
end
Hope you liked this blogs. For more blogs go to my profile
0 Comment(s)