Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Active Record To_hash

DZone's Guide to

Active Record To_hash

·
Free Resource
With this mix-in, you can have nice "to_hash" methods on your active record objects.  

I find it good for programming JSON apps.

In lib/ar_hashing.rb

module ActiveRecordHashing
  def to_hash
      Hash[*self.map{ |m| [m.id, m]}.flatten]               
  end
end


and in config/environment.rb

require 'ar_hashing'

class ActiveRecord::Associations::AssociationCollection
  include ActiveRecordHashing
end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}