/serialize-rails

rails attribute serialization into yaml, json, xml and with ruby marshal

Primary LanguageRubyMIT LicenseMIT

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 github.com/rails/rails/issues/10830#issuecomment-18883754

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
end

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.