<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: activerecord code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 12:39:29 GMT</pubDate>
    <description>DZone Snippets: activerecord code</description>
    <item>
      <title>Add a to_conditions method to ActiveRecord::Base for converting models to finder :conditions hash.</title>
      <link>http://snippets.dzone.com/posts/show/3732</link>
      <description>// Mixes in a to_conditions method to ActiveRecord::Base. Converts the attributes of an AR object to a&lt;br /&gt;// ActiveRecord::Base#find :conditions hash. Useful for comparing AR objects, especially when looking for&lt;br /&gt;// duplicates.&lt;br /&gt;// E.g.&lt;br /&gt;//   &lt;br /&gt;//   if not Post.find(:all, :conditions =&gt; my_post.conditions).empty?&lt;br /&gt;//     puts "Duplicate found"&lt;br /&gt;//   end&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Bezurk #:nodoc:&lt;br /&gt;  module ActiveRecord #:nodoc:&lt;br /&gt;    module Extensions&lt;br /&gt;      def to_conditions&lt;br /&gt;        attributes.inject({}) do |hash, (name, value)|&lt;br /&gt;          hash.merge(name.intern =&gt; value)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      alias :to_conditions_hash :to_conditions&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.send(:include, Bezurk::ActiveRecord::Extensions)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Mar 2007 10:54:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3732</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
  </channel>
</rss>
