1
votes

I'm trying to use embedded records in ember data and I think I'm missing something fundamental.

I have two models

app/models/video.js:

export default DS.Model.extend({
  title: DS.attr('string'),
  transcriptions: DS.hasMany('transcription', { embedded: 'always' })
});

app/models/transcription.js:

export default DS.Model.extend({
  video: DS.belongsTo('video')
});

I also have a custom serializer app/serializers/video.js:

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs:{
    transcriptions: { embedded: 'always' }
  },  
  extractSingle: function (store, type, payload, id) {
    var data = payload.data;
    return {
      id: data._id,
      title: data.Title,
      transcriptions: [{ id: "1" }] 
    }
  }
});

I would expect that this would result in my video model being populated with transcriptions being an array of transcription object but instead I get the following error:

"Error while processing route: videos.show" "Assertion Failed: Ember Data expected a number or string to represent the record(s) in the transcriptions relationship instead it found an object. If this is a polymorphic relationship please specify a type key. If this is an embedded relationship please include the DS.EmbeddedRecordsMixin and specify the transcriptions property in your serializer's attrs object."

Any suggestions of what I'm doing wrong here would be greatly appreciated.

UPDATE: The solution was to modify my custom serializer to the following:

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs:{
    transcriptions: { embedded: 'always' }
  },  
  extractSingle: function (store, type, payload, id) {
    var data = payload.data;

    var videoPayload = {
      id: data._id,
      title: data.Title,
      transcriptions: [{ id: "1" }] 
    };

    return this._super(store, type, videoPayload, id);
  }
}
1
Right - completely missed that - sorry I read the question too fast and focused on purely the error.. EmbeddedRecordsMixin - of course it's a complete record..jmurphyau
Yes, I've stripped out a bunch of stuff to try and focus on the cause. The transcription model will have other attributes etc. once I work out what's causing the error hereAdam Cooper

1 Answers

1
votes

The problem is the fact you're reimplementing extractSingle yourself.

You should call this.super if you're doing this..

In extractSingle on the REST Serializer it calls the normalize function - this normalise function is where the EmbeddedRecordsMixin does all it's work.

Because you're not calling either this.super or manually calling this.normalize you miss out on what the mixin is doing.