<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: to_hash code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 20 Aug 2008 20:27:48 GMT</pubDate>
    <description>DZone Snippets: to_hash code</description>
    <item>
      <title>Active Record to_hash</title>
      <link>http://snippets.dzone.com/posts/show/3082</link>
      <description>With this mix-in, you can have nice "to_hash" methods on your active record objects.  &lt;br /&gt;&lt;br /&gt;I find it good for programming JSON apps.&lt;br /&gt;&lt;br /&gt;In lib/ar_hashing.rb&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActiveRecordHashing&lt;br /&gt;  def to_hash&lt;br /&gt;      Hash[*self.map{ |m| [m.id, m]}.flatten]               &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and in config/environment.rb&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'ar_hashing'&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Associations::AssociationCollection&lt;br /&gt;  include ActiveRecordHashing&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Dec 2006 05:13:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3082</guid>
      <author>standsolid (Kenny)</author>
    </item>
  </channel>
</rss>
