# Save object and all relations

**URL:** <https://discuss.emberjs.com/t/save-object-and-all-relations/2602>\
**Category:** Ember Data\
**Created:** [September 12, 2013, 12:45pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602 "2013-09-12T12:45:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmathieu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dmathieu/32/14196_2.png) [@dmathieu](https://discuss.emberjs.com/u/dmathieu)\
**Post date:** [September 12, 2013, 12:45pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/1 "2013-09-12T12:45:10Z")

</div>

Hello,

Let’s suppose the following example:

```
App.Developer = DS.Model.extend({
  languages: DS.hasMany('language')
});

App.Language = DS.Model.extend({
  developer: DS.belongsTo('developer')
});

```

I want to be able to save a developer instance, and to automatically save all the associated languages at the same time.  
Is there any clean way to do this with the current status of ember-data, or do I have to loop through all the languages and manually save them after saving the developer?

---

<div class="post-metadata">

**Author:** ![feipinghuang](https://avatars.discourse-cdn.com/v4/letter/f/c89c15/32.png) [@feipinghuang](https://discuss.emberjs.com/u/feipinghuang)\
**Post date:** [September 12, 2013, 1:15pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/2 "2013-09-12T13:15:36Z")

</div>

Maybe that is you look for:

```
developer.save().then(function(model){
    return Ember.RSVP.all(model.get('languages').invoke('save'));
}).then(function(languages) {
  #transition the route  
})

```

---

<div class="post-metadata">

**Author:** ![dmathieu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dmathieu/32/14196_2.png) [@dmathieu](https://discuss.emberjs.com/u/dmathieu)\
**Post date:** [September 12, 2013, 2:17pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/3 "2013-09-12T14:17:43Z")

</div>

This could work. Unfortunately, `invoke` returns an array while I’d need a promise.  
And I’d need to be able to transition the route, but only once all saves have been done.

---

<div class="post-metadata">

**Author:** ![ptsd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ptsd/32/15018_2.png) [@ptsd](https://discuss.emberjs.com/u/ptsd)\
**Post date:** [September 14, 2013, 6:40pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/5 "2013-09-14T18:40:48Z")

</div>

should work. untested.

```auto
var promises = new Array();
    
developer.save().then(function(developer) {
    developer.get('languages').forEach(function(language) {
        if(language.get('isDirty') {
            promises.push(language.save());    
        }
    });
}, function(error) {
   //developer failed to save;
});

Ember.RSVP.all(promises).then(function() {
    //transition here
}, function(error) {
    //one or more languages failed to save
});

```

---

<div class="post-metadata">

**Author:** ![ptsd](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ptsd/32/15018_2.png) [@ptsd](https://discuss.emberjs.com/u/ptsd)\
**Post date:** [September 14, 2013, 6:58pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/6 "2013-09-14T18:58:02Z")

</div>

Looking at the ember/data issues, this was just commited: [https://github.com/emberjs/data/pull/1270](https://github.com/emberjs/data/pull/1270)

```auto
developer.save().then(function(developer) {
    developer.get('languages').save().then(function() {
            //transition here
        }, function(error) {
            //a language failed to save
    });
}, function(error) {
   //developer failed to save;
});

```

---

<div class="post-metadata">

**Author:** ![dmathieu](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dmathieu/32/14196_2.png) [@dmathieu](https://discuss.emberjs.com/u/dmathieu)\
**Post date:** [September 17, 2013, 9:43am UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/7 "2013-09-17T09:43:24Z")

</div>

Awesome. Thank you @ptsd.

---

<div class="post-metadata">

**Author:** ![rmcsharry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/rmcsharry/32/12570_2.png) [@rmcsharry](https://discuss.emberjs.com/u/rmcsharry)\
**Post date:** [September 18, 2016, 10:19am UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/8 "2016-09-18T10:19:39Z")

</div>

This appears to not work in EmberJS 2.7. It throws an error on getting the children:

`TypeError: newTrail.get(...).save is not a function`

```
newObject.save().then(function (newTrail) {
  newTrail.get('assignments').save().then(function() {
    console.log('DONE'); <-- eventually a transition here
  });
});

```

---

<div class="post-metadata">

**Author:** ![rmcsharry](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/rmcsharry/32/12570_2.png) [@rmcsharry](https://discuss.emberjs.com/u/rmcsharry)\
**Post date:** [September 18, 2016, 1:05pm UTC](https://discuss.emberjs.com/t/save-object-and-all-relations/2602/9 "2016-09-18T13:05:18Z")

</div>

`newTrail.get('assignments')` returns an array, so I cannot call save on an array.

The solution to my error is [here on SO.](http://stackoverflow.com/questions/39506375/emberjs-2-7-has-many-configuration-for-ember-data-and-active-model-serializers/39557875#39557875)
