class

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

new(of : Int32, start : DB::Any | Nil, finish : DB::Any | Nil, relation : QueryBuilder(T), cursor : String | Array(String) | Nil, order : Symbol | Array(Symbol), use_ranges : Bool | Nil)
Source

Instance methods

any?

Check if any batches exist

Source
average(column : String)

Get average across all batches (weighted by batch size)

Source
batch_size

The size of the batches yielded by the BatchEnumerator.

Source
count

Count total records across all batches

Source
delete_all

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.

Source
destroy_all

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.

Source
each

Yields a QueryBuilder object for each batch of records.

User.in_batches.each do |relation| relation.update_all(awesome: true) end

Source
each

Returns an Enumerator when no block is given

Source
each_record

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

Source
each_record

Returns an Enumerator when no block is given

Source
each_with_index

Enumerate over each batch relation with index

Source
empty?

Check if all batches are empty

Source
finish

The primary key value at which the BatchEnumerator ends, inclusive of the value.

Source
maximum(column : String)

Find maximum value across all batches

Source
minimum(column : String)

Find minimum value across all batches

Source
pluck(column : String)

Pluck values from all batches

Source
pluck(*columns : String)
Source
relation

The relation from which the BatchEnumerator yields batches.

Source
start

The primary key value from which the BatchEnumerator starts, inclusive of the value.

Source
sum(column : String)

Sum values across all batches

Source
update_all(attributes : Hash(String, DB::Any))

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.

Source
update_all
Source