Batched queries in rails

Posted by : on

Category : Rails

However in practice we need to perform certain task on each record and there are lots of records in the table. For this purpose we use batched queries in rails. Here is an example for the same:

What is batched queries:

Batched queries helps to select the records in batches. For example:

Order.find_each(batch_size: 50).map{|o| o.to_json }
#=> It will select all orders in batches like first batch will be 1-50 and second 51-100 etc.

You can use select_in_batches also for the same purpose.

Thanks for the reading !!!



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in