Flowdock

Notes posted by Fester

RSS feed
September 11, 2012 - (>= v3.1.0)
1 thank

Custom serialization

It is possible to supply a class with own (de)serialization logic to the serialize call. Given object must respond to load and dump calls.

Following example serializes symbols into their string representation and store them in database as raw strings instead of their YAML representation, i.e. :pumpkin would be stored as ‘pumpkin’, and not as ‘--- :pumpkin\n’

Example

clas SomeModel < ActiveRecord::Base
  class SymbolWrapper
    def self.load(string)
      string.to_sym
    end

    def self.dump(symbol)
      symbol.to_s
    end
  end

  serialize :value, SymbolWrapper
end