Skip to content

Instantly share code, notes, and snippets.

@ayamomiji
Created April 17, 2012 17:42
Show Gist options
  • Save ayamomiji/2407713 to your computer and use it in GitHub Desktop.
Save ayamomiji/2407713 to your computer and use it in GitHub Desktop.
rails serialize column in json
class JsonColumn
def self.default_with(&block)
new(block)
end
def initialize(default)
@default = default
end
def dump(obj)
MultiJson.dump(obj)
end
def load(json)
return @default.call if json.nil?
MultiJson.load(json)
end
end
class User < ActiveRecord::Base
serialize :detail, JsonColumn.default_with { Hash.new }
store_accessor :detail, :info, :birthday, :sex
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment