Takarik::Data::BatchEnumerator(T)
Inherits Enumerable < Reference < Object
BatchEnumerator provides enumerable methods for batched operations This is returned when in_batches is called without a block
Constructors
Instance methods
Deletes records in batches. Returns the total number of rows affected.
User.in_batches.delete_all
See QueryBuilder#delete_all for details of how each batch is deleted.
Destroys records in batches. Returns the total number of rows affected.
User.where("age < 10").in_batches.destroy_all
See QueryBuilder#destroy_all for details of how each batch is destroyed.
Yields a QueryBuilder object for each batch of records.
User.in_batches.each do |relation| relation.update_all(awesome: true) end
Looping through a collection of records from the database is very inefficient since it will try to instantiate all the objects at once.
In that case, batch processing methods allow you to work with the records in batches, thereby greatly reducing memory consumption.
User.in_batches.each_record do |user| user.do_awesome_stuff end
User.where("age > 21").in_batches(of: 10).each_record do |user| user.party_all_night! end
Updates records in batches. Returns the total number of rows affected.
User.in_batches.update_all(active: true)
See QueryBuilder#update_all for details of how each batch is updated.