Saving multiple hasMany


#1

It is odd how models with multiple hasMany are being saved. I’ve spend good amount of time trying to figure it out

let’s say I have following model:

window.App.Basket = DS.Model.extend({
        fruits: DS.hasMany('fruit', {
            async: true,
        }),
        vegetables: DS.hasMany('vegetable', {
            async: true,
        }),
});

And then I want to add another alement to fruits and save it:

window.App.FruitsRoute = window.Ember.Route.extend({
    actions: {
        addFruit: function (model) {
            var basket = this.modelFor('basket');
            basket.get('fruits').then(function (fruits) {
                fruits.pushObject(model);
                basket.save();
            });
        },
    },
});

Vegetables array posed to server will be empty! This behaviour seems very odd to me.

I’ve managed to solve it by adding custom save function (forcing model to load all relations) to basket model:

save_fixed: function () {

    // Forcing 'fruits' relation load before saving
    this.get('fruits').then(function () {

          // Forcing 'vegetables' relation load before saving
          return this.get('vegetables'));
    }).then(function () {

            // Saving parent only after all hasMany items has been loaded
            this.save();
        }.bind(this));
},

Ember Data version that I am using is 1.0.0-beta.7+canary.e3b896bc and Ember version 1.4.0

This issue is very similar to these issues on StackOverflow:

Is this a bug, or, if not, how it is supposed to work?


#2

Yeah I’ve had similar problems. It doesn’t really feel right, does it?

One thing you kind of left out of your description is which objects had already been persisted. I’ve had issues where I’ve been adding new children to a parent, and the parent wasn’t yet saved… so I save the children, but then when it comes time to saving the parent, it loses all its associations (it’s almost like the ids are being lost or something). This is actually a to-be-expected use case, though, because the server can’t know the parent id to write into the parent id of the child id if there isn’t one!

@alexspeller is this issue that @muchweb is talking about just because Ember-Data isn’t “finished” yet?


#3

Yes, currently if masMany associations weren’t loaded and I call save() on parent, it is posting [] as list of children which is false.

Server can’t tell if client really wants do remove all associations or value shouldn’t be updated.

Wouldn’t it be easier if Ember Data will skip the value, or send null instead of an empty array?


#4

Yeah, there’s an open issue or two about it:

Maybe you should mention your use case there… I think it’d be valuable for you to do so.

Actually, probably better to use the “mother” PR discussion:

Tho it seems it’ll be fixed by an upcoming version which is being called the “single source of truth” branch.

There’s also further discussion about it here: Ember Data fixture adapter saving record loses has many relationships