Subscribe to RSS Feed

Elijah Miller

01 Feb 2009

Making Rails' Serialize Even Better

Rails has this handy method that allows you store almost any object in the database with ease. Most often I end up using it for storing optional attributes in a hash.

Here is the proper syntax for telling Rails that there is an options attribute that should only store Hash values.

class User < ActiveRecord::Base
  serialize :options, Hash

The problem

The options attribute will start off as nil, and remain nil until you set it to something else. Setting the class_name to Hash only affects what you can write to this attribute.

>> user =
=> #<User id: nil, name: nil, options: nil>
>> user.options[:theme]
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
	from (irb):2
=> nil

The solution

What we really need is to automatically return an empty Hash on this new object so we can go on our merry way.

Add this to your environment.rb.

config.gem 'jqr-typed_serialize',
  :lib => 'typed_serialize',
  :source => ''

Now run this command to install the gem.

$ rake gems:install

A quick change of our model will fix all of our woes.

class User < ActiveRecord::Base
  typed_serialize :options, Hash
>> user =
=> #<User id: nil, name: nil, options: nil>
>> user.options[:theme]
=> nil
>> user.options
=> {}


The how and why

If you’re curious about how this works, I’ve written a simple post describing the the making of typed_serialize, or you can browse the code.