File: //usr/local/rvm/gems/ruby-2.5.9/doc/activerecord-6.1.4.1/ri/ActiveRecord/Batches/find_each-i.ri
U:RDoc::AnyMethod[iI"find_each:ETI"$ActiveRecord::Batches#find_each;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"?Looping through a collection of records from the database ;TI"F(using the Scoping::Named::ClassMethods.all method, for example) ;TI"Ris very inefficient since it will try to instantiate all the objects at once.;To:RDoc::Markup::BlankLine o;
; [I">In that case, batch processing methods allow you to work ;TI"Nwith the records in batches, thereby greatly reducing memory consumption.;T@o;
; [I"RThe #find_each method uses #find_in_batches with a batch size of 1000 (or as ;TI",specified by the +:batch_size+ option).;T@o:RDoc::Markup::Verbatim; [I""Person.find_each do |person|
;TI" person.do_awesome_stuff
;TI" end
;TI"
;TI"4Person.where("age > 21").find_each do |person|
;TI" person.party_all_night!
;TI" end
;T:@format0o;
; [I"OIf you do not provide a block to #find_each, it will return an Enumerator ;TI"%for chaining with other methods:;T@o;; [I"4Person.find_each.with_index do |person, index|
;TI"& person.award_trophy(index + 1)
;TI" end
;T;
0S:RDoc::Markup::Heading:
leveli : textI"Options;To:RDoc::Markup::List:
@type:BULLET:@items[
o:RDoc::Markup::ListItem:@label0; [o;
; [I"N<tt>:batch_size</tt> - Specifies the size of the batch. Defaults to 1000.;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"Dsize: it can be less than, equal to, 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"6# In worker 1, let's process until 9999 records.
;TI"1Person.find_each(finish: 9_999) do |person|
;TI" person.party_all_night!
;TI" end
;TI"
;TI"B# In worker 2, let's process from record 10_000 and onwards.
;TI"1Person.find_each(start: 10_000) do |person|
;TI" person.party_all_night!
;TI" end
;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"record;T[ I"S(start: nil, finish: nil, batch_size: 1000, error_on_ignore: nil, order: :asc);T@iFI"Batches;TcRDoc::NormalModule00