Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Created June 13, 2011 12:47
Show Gist options
  • Save Amitesh/1022715 to your computer and use it in GitHub Desktop.
Save Amitesh/1022715 to your computer and use it in GitHub Desktop.
Get hash of a model object
# For Ruby on Rails
# It will give hash of given model object
model_obj.serializable_hash
# Another way to get hash of a model object
ActiveSupport::JSON.decode(model_obj.to_json)
For MultiJson issue use
https://github.com/intridea/multi_json
require 'multi_json'
MultiJson.load('{"abc":"def"}') #=> {"abc" => "def"}
MultiJson.load('{"abc":"def"}', :symbolize_keys => true) #=> {:abc => "def"}
MultiJson.dump({:abc => 'def'}) # convert Ruby back to JSON
MultiJson.dump({:abc => 'def'}, :pretty => true) # encoded in a pretty form (if supported by the coder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment