<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: callbacks code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:24:49 GMT</pubDate>
    <description>DZone Snippets: callbacks code</description>
    <item>
      <title>ActiveRecord each_by_page</title>
      <link>http://snippets.dzone.com/posts/show/3557</link>
      <description>Perform an operation on a set of models including ActiveRecord callbacks, without instantiating all models at once.&lt;br /&gt;&lt;br /&gt;Step through and instantiate each member of the class and execute on it, but instantiate no more than per_page instances at any given time.&lt;br /&gt;Safe for destructive actions or actions that modify the fields your :order or :conditions clauses operate on.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  class Base&lt;br /&gt;    def each_by_page per_page, options = {}, &amp;block&lt;br /&gt;      # By-id for model-modifying blocks&lt;br /&gt;      # Build SQL to get ids of all matching records using the options provided by the user&lt;br /&gt;      sql = construct_finder_sql(options.dup.merge({ :select =&gt; 'id' }))&lt;br /&gt;      # Get the results as an array of tiny hashes { "id" =&gt; "1" } and flatten them out to just the ids&lt;br /&gt;      all_ids = connection.select_all(sql).map { |h| h['id'] }&lt;br /&gt;      at_a_time = 0..(per_page-1)&lt;br /&gt; &lt;br /&gt;      # chop apart the all_ids array a segment at a time&lt;br /&gt;      begin&lt;br /&gt;        ids = all_ids.slice!(at_a_time)&lt;br /&gt;        ids_cases = []&lt;br /&gt;        ids.each_with_index { |id, i| ids_cases &lt;&lt; "WHEN #{id} THEN #{i}" }&lt;br /&gt;        ids_cases = ids_cases.join(' ')&lt;br /&gt; &lt;br /&gt;        # Do the deed on this page of results&lt;br /&gt;        find(:all, options.merge(&lt;br /&gt;          :conditions =&gt; [ 'id IN (?)', ids ],&lt;br /&gt;          :order =&gt; "CASE id #{ids_cases} END"&lt;br /&gt;        )).each &amp;block&lt;br /&gt; &lt;br /&gt;      end until all_ids.empty?&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dweebd.com/ruby/activerecord-pagination-for-destructive-migrations/#comment-3"&gt;link to blog entry&lt;/a&gt;</description>
      <pubDate>Tue, 20 Feb 2007 23:59:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3557</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
  </channel>
</rss>
