# Saving a hasMany relationship with JSONAPI

**URL:** <https://discuss.emberjs.com/t/saving-a-hasmany-relationship-with-jsonapi/10971>\
**Category:** Ember Data\
**Created:** [June 19, 2016, 8:12pm UTC](https://discuss.emberjs.com/t/saving-a-hasmany-relationship-with-jsonapi/10971 "2016-06-19T20:12:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/benking/32/10616_2.png) [@benking](https://discuss.emberjs.com/u/benking)\
**Post date:** [June 19, 2016, 8:12pm UTC](https://discuss.emberjs.com/t/saving-a-hasmany-relationship-with-jsonapi/10971/1 "2016-06-19T20:12:40Z")

</div>

I’ve been trying to figure this out for a few hours now and it’s driving me crazy!

I simply want to allow Users to follow other Users. My User model looks like this;

```
export default Model.extend({
    name: attr('string'),
    username: attr('string'),
    avatar: belongsTo('image'),
    usersFollowing: hasMany('user')
});

```

When I get to creating a follow, I’ve tried this;

```
this.get('session.currentUser').get('usersFollowing').pushObject(userToFollow);
this.get('session.currentUser').save();

```

But that sends down my entire `usersFollowing` array to the server. This won’t be scalable. According to [the JSONAPI spec](http://jsonapi.org/format/#crud-updating-relationships), Ember Data should be making a POST request to the `/users-following` relationship link with just the data of the new follow.

Can anyone help with this? It seems like such a simple thing that I can’t get to work ☹

---

<div class="post-metadata">

**Author:** ![Diahron\_Grismore](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@Diahron\_Grismore](https://discuss.emberjs.com/u/Diahron_Grismore)\
**Post date:** [June 21, 2016, 3:42am UTC](https://discuss.emberjs.com/t/saving-a-hasmany-relationship-with-jsonapi/10971/2 "2016-06-21T03:42:19Z")

</div>

From your posting I can only guess you left a lot out. So, here’s a link that may help you best: [Relationships - Models - Ember Guides](https://guides.emberjs.com/v2.6.0/models/relationships/) , good luck.

---

<div class="post-metadata">

**Author:** ![benking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/benking/32/10616_2.png) [@benking](https://discuss.emberjs.com/u/benking)\
**Post date:** [July 8, 2016, 9:14pm UTC](https://discuss.emberjs.com/t/saving-a-hasmany-relationship-with-jsonapi/10971/3 "2016-07-08T21:14:41Z")

</div>

No, I’ve not left a lot out. I’ve read the guides, read the API, can’t find an answer to this.

If you want to add to a hasMany relationship, Ember is sending the entire children list to the server in a PATCH request. So for instance, say you’re adding a comment to a post that has thousands of comments, you’re going to be sending a payload with thousands of record objects (which will probably exceed HTTP limits).

I found [a GitHub issue](https://github.com/emberjs/data/issues/3733) that seems to match this problem, but it’s had no updates in over 9 months.

---

<div class="post-metadata">

**Author:** ![igorpreston](https://avatars.discourse-cdn.com/v4/letter/i/cc9497/32.png) [@igorpreston](https://discuss.emberjs.com/u/igorpreston)\
**Post date:** [July 11, 2016, 12:06pm UTC](https://discuss.emberjs.com/t/saving-a-hasmany-relationship-with-jsonapi/10971/4 "2016-07-11T12:06:15Z")

</div>

I think you should first add inverse parent-child relationship on User model. You defined that User has many Users via usersFollowing, but there is no belongsTo defined on User model to refer to foreign key.

```
export default Model.extend({
    name: attr('string'),
    username: attr('string'),
    avatar: belongsTo('image'),
    followee: belongsTo('user'),
    followers: hasMany('user', { inverse: 'followee' })
});

```

Also, not sure what api backend you’re using, but if it’s Rails, then you need to define two has\_many through relationship with join table on same model with two foreign keys pointing to that model.
