Configure Mongoid with Serialization support in Rails Controllers.
id as an available attribute, which can be used as the primary key as is expected by most API clients.
Inspired by the discussion here: http://stackoverflow.com/questions/11571777/ember-data-and-mongodb-how-to-handle-id
Note: Should work for Mongoid 3+
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install mongoid-serializer
class UserSerializer < ActiveModel::MongoidSerializer attributes :id, :first_name, :last_name, :quote, :age, http_status end
Additionally this gem includes a
ActiveModel::StateTracking module. This modules provides the methods
deleted? for use with any Mongoid Document.
You can either include this module in models of your choice, or include it into
Mongoid::Document to make it part of every model.
ActiveModel::StatusSerializer is configured to try to hook into the
StateTracking methods from active_model-shaz and set the JSON
http_status key to either
204, to reflect created or deleted state back to the client.
ActiveModel::StatusSerializer and sets the
id attribute to point to
_id to better work with what the usual client side APIs expect (f.ex for use with ember-data).
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request