Skip to content

Instantly share code, notes, and snippets.

@csquared
Created February 2, 2012 20:27
Show Gist options
  • Save csquared/1725571 to your computer and use it in GitHub Desktop.
Save csquared/1725571 to your computer and use it in GitHub Desktop.
Serializes JSON
# 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