Skip to content

Instantly share code, notes, and snippets.

@ssaunier
Last active May 15, 2018 18:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaunier/3866812 to your computer and use it in GitHub Desktop.
Save ssaunier/3866812 to your computer and use it in GitHub Desktop.
Recurively symbolize ruby object keys
module Symbolizer
def self.deep_symbolize(object)
if object.kind_of? Hash
object.keys.each do |k|
object[k == "_id" ? :id : k.to_sym] = self.deep_symbolize(object.delete k)
end
object
elsif object.kind_of? Array
object.map { |e| self.deep_symbolize e }
else
object
end
end
end
@ssaunier
Copy link
Author

Little trick to convert mongoid "_id" to symbol :id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment