Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Created June 13, 2011 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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