I'm using ember-data with rails and MongoDB and am having problem with the way IDs are stored in MongoDB - in a _id field.
Ember-data will use id as the default field for ID so I tried to override it like this:
App.User = DS.Model.extend
primaryKey: "_id"
name: DS.attr "string"
image: DS.attr "string"
This seems to work most of the time but in some instances I get exceptions from ember saying:
Uncaught Error: assertion failed: Your server returned a hash with the key _id but you have no mappings
I suspect this might be a bug in ember-data because it's still heavily under development, but I was trying to find a way to get to map _id to id on the server side in rails? I'm using mongoid to do the mongo mapping.
Ahh, instead of including _id in your JSON, you could craft the JSON to instead use the id method rather than the _id attribute. Ways:
You could use rabl, and the JSON could be like:
You could also craft the as_json method
I had a similar problem using ember.js with ember-resource and couchdb, which also stores it's IDs as
_id
.As solution to this problem I defined a superclass for all my model classes containing a computed property to duplicate
_id
intoid
like this:Maybe this could solve your problem too?
If you use Mongoid3, here is the monkey patch may work for you.
https://gist.github.com/4700909