Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created August 15, 2011 09:24
Show Gist options
  • Save mindscratch/1145952 to your computer and use it in GitHub Desktop.
Save mindscratch/1145952 to your computer and use it in GitHub Desktop.
Mongoid: return 'id' instead of '_id' when serializing documents as JSON
module Mongoid
module Document
def as_json(options={})
attrs = super options
attrs["id"] = attrs.delete "_id" if attrs.has_key? "_id"
attrs
end
end
end
@mindscratch
Copy link
Author

In a Rails application I place this file in config/initializers

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