File: //usr/local/rvm/gems/ruby-2.5.9/doc/activerecord-6.1.4.1/ri/ActiveRecord/Batches/in_batches-i.ri
U:RDoc::AnyMethod[iI"in_batches:ETI"%ActiveRecord::Batches#in_batches;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"KYields ActiveRecord::Relation objects to work with a batch of records.;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [ I"7Person.where("age > 21").in_batches do |relation|
;TI" relation.delete_all
;TI"/ sleep(10) # Throttle the delete queries
;TI" end
;T:@format0o;
; [I"DIf you do not provide a block to #in_batches, it will return a ;TI")BatchEnumerator which is enumerable.;T@o;; [ I"BPerson.in_batches.each_with_index do |relation, batch_index|
;TI"2 puts "Processing relation ##{batch_index}"
;TI" relation.delete_all
;TI" end
;T;
0o;
; [I"HExamples of calling methods on the returned BatchEnumerator object:;T@o;; [I""Person.in_batches.delete_all
;TI"1Person.in_batches.update_all(awesome: true)
;TI"7Person.in_batches.each_record(&:party_all_night!)
;T;
0S:RDoc::Markup::Heading:
leveli : textI"Options;To:RDoc::Markup::List:
@type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o;
; [I"F<tt>:of</tt> - Specifies the size of the batch. Defaults to 1000.;To;;0; [o;
; [I"T<tt>:load</tt> - Specifies if the relation should be loaded. Defaults to false.;To;;0; [o;
; [I"]<tt>:start</tt> - Specifies the primary key value to start from, inclusive of the value.;To;;0; [o;
; [I"Z<tt>:finish</tt> - Specifies the primary key value to end at, inclusive of the value.;To;;0; [o;
; [I"o<tt>:error_on_ignore</tt> - Overrides the application config to specify if an error should be raised when ;TI")an order is present in the relation.;To;;0; [o;
; [I"`<tt>:order</tt> - Specifies the primary key order (can be :asc or :desc). Defaults to :asc.;T@o;
; [I"NLimits are honored, and if present there is no requirement for the batch ;TI"Asize, it can be less than, equal, or greater than the limit.;T@o;
; [
I"HThe options +start+ and +finish+ are especially useful if you want ;TI"Kmultiple workers dealing with the same processing queue. You can make ;TI"Hworker 1 handle all the records between id 1 and 9999 and worker 2 ;TI"Hhandle from 10000 and beyond by setting the +:start+ and +:finish+ ;TI"option on each worker.;T@o;; [I",# Let's process from record 10_000 on.
;TI"@Person.in_batches(start: 10_000).update_all(awesome: true)
;T;
0o;
; [I">An example of calling where query method on the relation:;T@o;; [
I"*Person.in_batches.each do |relation|
;TI", relation.update_all('age = age + 1')
;TI"A relation.where('age > 21').update_all(should_party: true)
;TI". relation.where('age <= 21').delete_all
;TI" end
;T;
0o;
; [I"LNOTE: If you are going to iterate through each record, you should call ;TI"1#each_record on the yielded BatchEnumerator:;T@o;; [I"7Person.in_batches.each_record(&:party_all_night!)
;T;
0o;
; [ I"[NOTE: Order can be ascending (:asc) or descending (:desc). It is automatically set to ;TI".ascending on the primary key ("id ASC"). ;TI"IThis also means that this method only works when the primary key is ;TI"+orderable (e.g. an integer or string).;T@o;
; [I"LNOTE: By its nature, batch processing is subject to race conditions if ;TI"0other processes are modifying the database.;T:
@fileI"*lib/active_record/relation/batches.rb;T:0@omit_headings_from_table_of_contents_below00I"yielded_relation;T[ I"X(of: 1000, start: nil, finish: nil, load: false, error_on_ignore: nil, order: :asc);T@tFI"Batches;TcRDoc::NormalModule00