Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Created April 11, 2012 10:24
Show Gist options
  • Save tobiashm/2358486 to your computer and use it in GitHub Desktop.
Save tobiashm/2358486 to your computer and use it in GitHub Desktop.
module ModelSerializer
def self.serialize(object)
object.to_json include: nil # don't include any associations - only serialize shallow object
end
def self.deserialize(json)
hash = ActiveSupport::JSON.decode(json) # => { "section": { "id": 42, ... } }
model = hash.keys.first.camelize.constantize # => Section
attributes = hash.values.first
object = model.new
object.assign_attributes attributes, without_protection: true
object
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment