hasMany relationship not being saved

I’ve been struggling all day with this. Read a ton of documentation, tried several different ways and still no luck. Hope someone can help me out. I’m using REST adapter & serializer.

The problem is I cannot persist models that reference each other:

models/car.js

export default DS.Model.extend({
  name: DS.attr('string'),
  wheels: DS.hasMany('wheel', { async: true })
});

models/wheel.js

export default DS.Model.extend({
  name: DS.attr('string'),
  car: DS.belongsTo('car')
});

Then I have the following route action:

addWheel() {
    var model = this.get('controller.model');
    this.store.createRecord('wheel', {
        name: 'New wheel'
    }).save().then(function(wheel) {
        model.get('wheels').then(function(wheels) {
            wheels.pushObject(wheel);
            model.save();
        });
    });
}

The wheel object gets saved, but when I save the car object it doesn’t send the hasMany array. These are the requests that go to the server:

POST /api/wheels

Request:

{"wheel":{"name":"New wheel","car":"car.1"}}

Response:

{"wheel":{"name":"New wheel","car":"car.1","_id":"55eca8d731cff3488f6a3862"}}

PUT /api/cars/car.1

Request:

{"car":{"name":"Audi"}}

Question:

Why is it not sending hasMany relationships? How do I persist data like this?

1 Like

bump :smile: Nobody ever ran into this issue before? Sounds like a pretty common scenario to me.

At the end of my second day googling I found this post:

Here’s what it says about saving hasMany relationships:

Note though that, by default, RESTSerializer, will not add DS.hasMany
 associated IDs to the objects that it serializes, since those 
associations are specified on the “many” side (i.e., those which have a DS.belongsTo association).  So, in our example, although a Post has many comments, those IDs will not be added to the Post object

This is totally unintuitive. Not only it is not documented anywhere, there is no clear solution either. Should I update the “many” side in the backend and refresh the record afterwards? What they suggest is using the DS.EmbeddedRecordsMixin which will serialize hasMany relationships by default. 2 days wasted on such a trivial thing. Is it just me constantly hitting the wall with Ember Data?

1 Like

You should use the EmbeddedRecordsMixin, something like this:

export default ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin).extend({
    attrs: {
        items: {
            serialize: 'records', 
            deserialize: 'ids'
        }
     }
});

That will serialize records when sending them to your backend and deserialize them as IDs.

2 days wasted on such a trivial thing. Is it just me constantly hitting the wall with Ember Data?

ED has its rough parts, but there have been a lot of improvements recently, and the team is working on it. The API to extend it and make it suit your API needs is excellent… the documentation can be a little better, I agree.

For anyone using JSON API the solution is outlined here

2 Likes