Save hasmany relationship to server at once

Hello,

I’ve allready asked a similar question here (you can look at it to see my code example) and certainly stackoverflow is a better place to this type of question, but since I didn’t get any answer (and search many hours on the web) and while my question is about “best practices” may be you could help me there ?

I have 2 models with hasmany and belongTo relationship, and I want to save all the change made in the store to server when user click on a “save” button. The problem is that when the “parent” model is saved he don’t allready know the ids of his hasmany childs relationship as they are not allready saved, and vis versa.

I found some old toppic about this problem, but don’t know what is the actual evolution of ember-data about this : [RESTAdapter] Allow new parent, child to be saved at once

DS.RESTAdapter: Robust Support for Parent->Child Hierarchies

I saw “store.commit” but it seem to be outdated ?, may be there is an other way to “magicaly” resolve the save “id” problem on server. Otherwise what is the best pratice to do that manualy ?

And to finish, I’m begginer and it is the first time a get so much difficulty to get something to work with Ember, may be there is a lake about this in the official Ember documentation ?

Sorry my bad english, hope my question is still understandable.

1 Like

There is no way of saving all in one request, but to embed the children and let the backend handle the rest. EmbeddedRecordsMixin - 4.6 - Ember API Documentation.

Another option is to save the parent and then save the children, when the parents id has returned.

parent.save().then(function(savedParent) {
  savedParent.get('children').invoke('save');
});

Thank you for your anwser !

parent.save().then(function(savedParent) {
   savedParent.get('children').invoke('save');
});

I’ve tryed your code but the problem still persist, when the parent is saved he don’t know allready the list of children ids and it produce a error.

I will try to understand embedded records…

I did a try like that : save all childs without ID of the parent, then save parent and then saved again childs to set the parent id, witch seem to work, but the code look very ugly :

App.Row = DS.Model.extend({
   columns:  DS.hasMany('column', {async: true}),
});

App.Column = DS.Model.extend({
   row: DS.belongsTo('row', {async: true}),
});

App.RowSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
   attrs: { columns: {serialize: 'ids', deserialize: 'ids'} }
});

...
save: function(){
    var row  = this.store.all('row');
    
    row.forEach(function(r, index, self){
        var promises = Ember.A();
        r.get('columns').forEach(function(c){
            promises.push(c.save());
        })
        Ember.RSVP.Promise.all(promises).then(function(resolvedPromises){
            r.save().then(function(rSaved){
                rSaved.get('columns').forEach(function(c){
                     c.set('row',rSaved);
                     c.save();
                })
             });
         });
     })
},

hello guys i’m try to make hasMany related model save in single request on JSON api adapter. but it encounter many problems. in my see request payload is json api formatted. how to solve it?