class ActiveRecord::Batches::BatchEnumerator
def batch_size
def batch_size @of end
def delete_all
Person.in_batches.delete_all
Deletes records in batches. Returns the total number of rows affected.
def delete_all sum(&:delete_all) end
def destroy_all
Person.where("age < 10").in_batches.destroy_all
Destroys records in batches.
def destroy_all each(&:destroy_all) end
def each(&block)
relation.update_all(awesome: true)
Person.in_batches.each do |relation|
Yields an ActiveRecord::Relation object for each batch of records.
def each(&block) enum = @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false) return enum.each(&block) if block_given? enum end
def each_record(&block)
person.award_trophy(index + 1)
Person.in_batches.each_record.with_index do |person, index|
for chaining with other methods:
If you do not provide a block to #each_record, it will return an Enumerator
end
person.party_all_night!
Person.where("age > 21").in_batches(of: 10).each_record do |person|
end
person.do_awesome_stuff
Person.in_batches.each_record do |person|
records in batches, thereby greatly reducing memory consumption.
In that case, batch processing methods allow you to work with the
instantiate all the objects at once.
+all+ method, for example) is very inefficient since it will try to
Looping through a collection of records from the database (using the
def each_record(&block) return to_enum(:each_record) unless block_given? @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: true).each do |relation| relation.records.each(&block) end end
def initialize(of: 1000, start: nil, finish: nil, relation:) # :nodoc:
def initialize(of: 1000, start: nil, finish: nil, relation:) # :nodoc: @of = of @relation = relation @start = start @finish = finish end
def update_all(updates)
Person.in_batches.update_all("age = age + 1")
Updates records in batches. Returns the total number of rows affected.
def update_all(updates) sum do |relation| relation.update_all(updates) end end