Created
February 2, 2012 20:27
-
-
Save csquared/1725571 to your computer and use it in GitHub Desktop.
Serializes JSON
This file contains hidden or 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
# serialize :field, Hash | |
# got you down? | |
# serializes_json :field | |
# to the rescue! | |
class ActiveRecord::Base | |
def self.serializes_json *args | |
args.each do |field_name| | |
eval <<-RUBY | |
def #{field_name}=(other) | |
self['#{field_name}'] = other.to_json | |
end | |
def #{field_name} | |
JSON.parse(self['#{field_name}'] || '{}') | |
end | |
RUBY | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment