<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActiveRecord::Batches::ClassMethods</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActiveRecord::Batches::ClassMethods</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/active_record/batches_rb.html"> lib/active_record/batches.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <p> When processing large numbers of records, it‘s often a good idea to do so in batches to prevent memory ballooning. </p> </div> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000389">find_each</a> <a href="#M000390">find_in_batches</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000389" class="method-detail"> <a name="M000389"></a> <div class="method-heading"> <a href="ClassMethods.src/M000389.html" target="Code" class="method-signature" onclick="popupCode('ClassMethods.src/M000389.html');return false;"> <span class="method-name">find_each</span><span class="method-args">(options = {}) {|record| ...}</span> </a> </div> <div class="method-description"> <p> Yields each record that was found by the find <tt>options</tt>. The find is performed by <a href="ClassMethods.html#M000390">find_in_batches</a> with a batch size of 1000 (or as specified by the <tt>:batch_size</tt> option). </p> <p> Example: </p> <pre> Person.find_each(:conditions => "age > 21") do |person| person.party_all_night! end </pre> <p> Note: This method is only intended to use for batch processing of large amounts of records that wouldn‘t fit in memory all at once. If you just need to loop over less than 1000 records, it‘s probably better just to use the regular find methods. </p> </div> </div> <div id="method-M000390" class="method-detail"> <a name="M000390"></a> <div class="method-heading"> <a href="ClassMethods.src/M000390.html" target="Code" class="method-signature" onclick="popupCode('ClassMethods.src/M000390.html');return false;"> <span class="method-name">find_in_batches</span><span class="method-args">(options = {}) {|records| ...}</span> </a> </div> <div class="method-description"> <p> Yields each batch of records that was found by the find <tt>options</tt> as an array. The size of each batch is set by the <tt>:batch_size</tt> option; the default is 1000. </p> <p> You can control the starting point for the batch processing by supplying the <tt>:start</tt> option. This is especially useful if you want multiple workers dealing with the same processing queue. You can make worker 1 handle all the records between id 0 and 10,000 and worker 2 handle from 10,000 and beyond (by setting the <tt>:start</tt> option on that worker). </p> <p> It‘s not possible to set the order. That is automatically set to ascending on the primary key ("id ASC") to make the batch ordering work. This also mean that this method only works with integer-based primary keys. You can‘t set the limit either, that‘s used to control the the batch sizes. </p> <p> Example: </p> <pre> Person.find_in_batches(:conditions => "age > 21") do |group| sleep(50) # Make sure it doesn't get too crowded in there! group.each { |person| person.party_all_night! } end </pre> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>