Created
April 17, 2012 17:42
-
-
Save ayamomiji/2407713 to your computer and use it in GitHub Desktop.
rails serialize column in json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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