<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: find code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 15:58:04 GMT</pubDate>
    <description>DZone Snippets: find code</description>
    <item>
      <title>Perform a Rails find() and iterate over the resulting records in groups</title>
      <link>http://snippets.dzone.com/posts/show/5461</link>
      <description>&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  class Base&lt;br /&gt;    # This method lets you iterate over the results of a .find, in groups.&lt;br /&gt;    # (Basically an interface to LIMIT.)&lt;br /&gt;    # Anything you can pass as options to .find, you can pass here. &lt;br /&gt;    # Example 1:&lt;br /&gt;    #   Order.each_by(100, :conditions =&gt; { :cc_processed_at =&gt; nil }) do |order|&lt;br /&gt;    #     # do stuff with order&lt;br /&gt;    #   end&lt;br /&gt;    # Example 2:&lt;br /&gt;    #   Person.each_by(50, :order =&gt; 'name') do |person, index|&lt;br /&gt;    #     # do stuff with person and index&lt;br /&gt;    #   end&lt;br /&gt;    # Pass :update =&gt; true in the options to print a message before each group is&lt;br /&gt;    # fetched from the db.&lt;br /&gt;    #&lt;br /&gt;    # Author: Elliot Winkler &lt;elliot.winkler@gmail.com&gt;&lt;br /&gt;    # Source: http://snippets.dzone.com/posts/show/5461&lt;br /&gt;    def self.each_by(group_size, options={}, &amp;blk)&lt;br /&gt;      update = options.delete(:update) || false&lt;br /&gt;      num_records = count(options.except(:from))&lt;br /&gt;      return 0 if num_records == 0&lt;br /&gt;      #raise "Number of records: #{num_records}"&lt;br /&gt;      also_pass_offset = (blk.arity == 2)&lt;br /&gt;      0.step(num_records, group_size) do |offset|&lt;br /&gt;        find_options = { :offset =&gt; offset, :limit =&gt; group_size }.merge(options)&lt;br /&gt;        if update&lt;br /&gt;          if num_records == 1&lt;br /&gt;            puts "&gt;&gt; Reading the only record."&lt;br /&gt;          else&lt;br /&gt;            start_offset = offset + 1&lt;br /&gt;            end_offset   = offset + group_size&lt;br /&gt;            end_offset   = num_records if num_records &lt; end_offset&lt;br /&gt;            puts "&gt;&gt; Reading records #{start_offset}-#{end_offset}."&lt;br /&gt;          end&lt;br /&gt;        end &lt;br /&gt;        find(:all, find_options).each do |record|&lt;br /&gt;          also_pass_offset ? blk.call(record, offset) : blk.call(record)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      num_records&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Also see:&lt;br /&gt;&lt;br /&gt;* http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord&lt;br /&gt;* http://weblog.jamisbuck.org/2007/2/28/poor-man-s-pagination</description>
      <pubDate>Mon, 05 May 2008 15:35:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5461</guid>
      <author>mcmire ()</author>
    </item>
  </channel>
</rss>
