# Saving object with relationships

**URL:** <https://discuss.emberjs.com/t/saving-object-with-relationships/8842>\
**Category:** Ember Data\
**Created:** [September 24, 2015, 9:18pm UTC](https://discuss.emberjs.com/t/saving-object-with-relationships/8842 "2015-09-24T21:18:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yaqwsx](https://avatars.discourse-cdn.com/v4/letter/y/90ced4/32.png) [@yaqwsx](https://discuss.emberjs.com/u/yaqwsx)\
**Post date:** [September 24, 2015, 9:18pm UTC](https://discuss.emberjs.com/t/saving-object-with-relationships/8842/1 "2015-09-24T21:18:00Z")

</div>

Hello,

I am implement a discussion forum in my Ember App. I use Ember-Data with RESTAdapter for accessing the backend. I have a post model and a thread model. Thread can hasMany root posts. When I create new thread, I need to create thread and the root post.

I tried to create new record of thread with new record of post embedded and it didn’t work. Ember replaced the post with null. So when i called save on thread, I only saved the thread without any realtionship (and also no post).

What is the correct way to save thread and a post? Or what is the correct way of saving multiple models with non-trivial relationships?

---

<div class="post-metadata">

**Author:** ![rytis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/rytis/32/11242_2.png) [@rytis](https://discuss.emberjs.com/u/rytis)\
**Post date:** [September 25, 2015, 8:54am UTC](https://discuss.emberjs.com/t/saving-object-with-relationships/8842/2 "2015-09-25T08:54:09Z")

</div>

Save the children first. Funny how this sounds like a protocol for abandoning a ship 😃 Here’s an example:

```
var store = this.store;
var post = store.createRecord('post', { title: 'First post!' });
post.save().then(function() {
    return store.createRecord('thread', { title: 'New thread', posts:[post] }).save();
});

```

In my last project I created a mixin that extends an ED model with saveRelationships() & rollbackRelationships() methods. If you think about the model and its relationships it’s a tree. I would recursively traverse the tree and save leaf nodes first. Works perfectly with async properties too. This makes saving even the deeply nested models a breeze. To translate that to your case:

```
var thread = this.store.createRecord('thread', {
    title: 'New thread',
    posts: [
        this.store.createRecord('post', { title: 'New post!' })
    ]
});
thread.saveRelationships().then(function() {
    return thread.save()
});

```

Wish something like this was built in.

---

<div class="post-metadata">

**Author:** ![toranb](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/toranb/32/16579_2.png) [@toranb](https://discuss.emberjs.com/u/toranb)\
**Post date:** [September 25, 2015, 4:57pm UTC](https://discuss.emberjs.com/t/saving-object-with-relationships/8842/3 "2015-09-25T16:57:17Z")

</div>

Is ember-data a hard requirement? We had this same need (for embedded POST) and manage the relationship(s) ourselves (allowing any type of ajax POST/etc).

Beyond creating /managing the one-to-many relationship yourself you need to decide on a primary key strategy. We elected to have ember generate a true uuid value (so when we POST we already have a PK value for both parent/and nested models)

If you can’t do this it becomes increasingly more difficult as you need to use something like a “temp” pk until the backend resolves/ then “flip” the pks to reflect what is truly persisted on the server.
