How do I handle broken belongTo relationships?

Due to a bug in our API server, Ember Data is creating records that doesn’t exist. It’s a bit difficult to explain, so I’ll provide an example. Let’s say we have these models.

models/foo.js

export default DS.Model.extend({
  bar: DS.belongsTo(‘bar’, { async: false });
});

models/bar.js

export default DS.Model.extend({
  someAttr: DS.attr()
});

When we do a DS.store.findAll on foo and bar, the API would (occassionally) return something like this: http://codebeautify.org/jsonviewer/cb54318b

This causes Ember Data to create a foo record with ID of “foo2”, which has all of its attributes being undefined. Is there a way to let Ember Data know that “foo2” does not exist, and prevent the creation of a phantom record?

Thanks.

Yes, this is possible by overriding normalizeResponse (or more accurately normalizeFindAllResponse) in your serializer. If it’s not specific to neither foo nor bar, you can do this in the application serializer.

You would basically check for multiple records in the payload and discard accordingly.