Skip to content
rails attribute serialization into yaml, json, xml and with ruby marshal
Latest commit bb6b14b Jan 15, 2015 @mrbrdo Merge pull request #3 from shaggyone/fix_for_issue_2
Fix for issue #2
Failed to load latest commit information.


Serialize for Rails

Rails 4 note

There were some internal changes in Rails 4 regarding the serialize method. These changes seem to affect how serialize works in combination with binary column types such as bytea. For the moment I do not recommend using binary column types in combination with this gem on Rails 4. If you want to know more see

Description and use

Rails already supports serialization of attributes, but it can only do Yaml. This gem upgrades the default Rails serialize method so you can serialize into yaml, json and with marshal (Ruby binary format). It also supports gzipping the output if you want it to take up less space.

Format can be one of the following:

:yaml, :json, :marshal

Yaml is the default, and serialize will behave just like normal if you don't use any of the new options.

Example usage:

class Mouse < ActiveRecord::Base
  serialize :info, Hash, :format => :json
  serialize :data, :format => :yaml, :gzip => true

When you use gzip you must use a binary-compatible column type such as `BLOB` (MySQL) or `bytea` (PostgreSQL). If you don't use gzip, `text` is recommended to allow strings of arbitrary length.

Something went wrong with that request. Please try again.