Skip to content

Instantly share code, notes, and snippets.

@Schniz
Last active August 29, 2015 13:57
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 Schniz/9558489 to your computer and use it in GitHub Desktop.
Save Schniz/9558489 to your computer and use it in GitHub Desktop.
I've seen it somewhere.. and I've added the 'date_created' thing that you can't access when you're doing it all RESTy.
# Adds "id" and "date_created" to the mongoid document.
# "id" => "_id"
# "date_created" => "_id.generation_time"
# YEAH. thats it.
module Mongoid
module Document
def as_json(options={})
attrs = super(options)
attrs["id"] = self.persisted? ? self._id : nil
attrs["date_created"] = self.persisted? ? self._id.generation_time : nil
attrs
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment