# How to handle lots of relationships?

**URL:** <https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562>\
**Category:** Ember Data\
**Created:** [August 12, 2015, 10:57pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562 "2015-08-12T22:57:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZebraFlesh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/zebraflesh/32/10762_2.png) [@ZebraFlesh](https://discuss.emberjs.com/u/ZebraFlesh)\
**Post date:** [August 12, 2015, 10:57pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/1 "2015-08-12T22:57:48Z")

</div>

What’s a good pattern for dealing with a large number of models on the other side of a hasMany relationship? For example, if I have a hasMany that would resolve into 1 million models and I’m using the RESTAdapter/RESTSerializer, it seems like a bad idea to just hand back all 1 million ids as a field in the main belongsTo model.

So what should I do instead? I’m scratching my head on this one.

---

<div class="post-metadata">

**Author:** ![Wayne\_Douglas](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/wayne_douglas/32/9568_2.png) [@Wayne\_Douglas](https://discuss.emberjs.com/u/Wayne_Douglas)\
**Post date:** [August 13, 2015, 10:00am UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/2 "2015-08-13T10:00:06Z")

</div>

I suspect you wouldn’t want to model the relationship and handle loading the relations manually with pagination. Could be wrong though 😛

---

<div class="post-metadata">

**Author:** ![ZebraFlesh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/zebraflesh/32/10762_2.png) [@ZebraFlesh](https://discuss.emberjs.com/u/ZebraFlesh)\
**Post date:** [August 13, 2015, 12:26pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/3 "2015-08-13T12:26:55Z")

</div>

Probably not. I’m aiming more for a solution where the paging is in the route. The user is investigating the results of a batch process so they’d hit a url similar to /myapp/batch/someBatchId/1. This would show them summary data about the batch as well as the first page of data (likely side loaded in the response for the batch summary data).

I was reading that this is what the links response is for, so I might try that.

---

<div class="post-metadata">

**Author:** ![Wayne\_Douglas](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/wayne_douglas/32/9568_2.png) [@Wayne\_Douglas](https://discuss.emberjs.com/u/Wayne_Douglas)\
**Post date:** [August 14, 2015, 10:18am UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/4 "2015-08-14T10:18:30Z")

</div>

SailsJs + these blueprints gets you links quick if you want to spike some code:

[https://github.com/mphasize/sails-generate-ember-blueprints](https://github.com/mphasize/sails-generate-ember-blueprints)

---

<div class="post-metadata">

**Author:** ![onsmith](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/onsmith/32/13462_2.png) [@onsmith](https://discuss.emberjs.com/u/onsmith)\
**Post date:** [March 1, 2017, 10:21pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/5 "2017-03-01T22:21:43Z")

</div>

I’d like to resurrect this post. I’m also interested in how to deal with large relationships in ember-data.

I feel that as part of the “ambitious web apps” mantra, there should be some sort of recommended technique for dealing with this problem.

An ideal technique would allow developers to model a large `hasMany` relationship (1000+ related records) with ember-data without having to pass a long list of ids with the parent record. Additionally, when it is time to load related records, a recommended technique should exist for requesting filtered, paginated, or sorted records from the server.

Is the links response the solution for this problem? If so, does anyone know of a tutorial/blog post that they could direct me to? I’m especially interested in how to paginate/filter/sort the large hasMany records server-side.

Thanks!

---

<div class="post-metadata">

**Author:** ![francesconovy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/francesconovy/32/9607_2.png) [@francesconovy](https://discuss.emberjs.com/u/francesconovy)\
**Post date:** [March 2, 2017, 3:42pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/6 "2017-03-02T15:42:56Z")

</div>

In our projects, for cases like this we do not resolve the relationship via Ember-Data, but (as mentioned as a possible solution above), simply query the API accordingly.

So instead of doing: `item.get('subitems')`, we do `this.get('store').query('subitems', { item: item.get('id') }`, which then allows us to filter, sort, paginate etc. as we want.

If we want to filter/sort/paginate a set of items, I don’t really see the advantage of using an automatically resolving relationship - If I resolve a relationship, I’d expect to get _all_ records.

---

<div class="post-metadata">

**Author:** ![palmergs](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/palmergs/32/9713_2.png) [@palmergs](https://discuss.emberjs.com/u/palmergs)\
**Post date:** [March 2, 2017, 5:22pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/7 "2017-03-02T17:22:26Z")

</div>

> If I resolve a relationship, I’d expect to get all records.

I think this is the fundamental insight. It took me a long time to wrap my head around this since so many of the beginning tutorials are eager to show off the automatic relationships, but for large relationships I use an “old fashioned” query and a route + controller using `queryParams`.

---

<div class="post-metadata">

**Author:** ![onsmith](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/onsmith/32/13462_2.png) [@onsmith](https://discuss.emberjs.com/u/onsmith)\
**Post date:** [March 3, 2017, 2:08am UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/8 "2017-03-03T02:08:00Z")

</div>

Thank you both so much for your input!

I can’t believe I didn’t think of that, but `this.get('store').query('children', { parent_id: parent.get('id') }` would totally work.

Does this mean you typically avoid `parent.get('children')` in your code (since this would resolve the relationship)? As a workaround, do you regularly employ the [ds-references](http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code) ember-data API to fetch already-loaded relationship records without resolving the entire relationship?

Thanks again!

---

<div class="post-metadata">

**Author:** ![francesconovy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/francesconovy/32/9607_2.png) [@francesconovy](https://discuss.emberjs.com/u/francesconovy)\
**Post date:** [March 6, 2017, 12:58pm UTC](https://discuss.emberjs.com/t/how-to-handle-lots-of-relationships/8562/9 "2017-03-06T12:58:51Z")

</div>

It depends. Generally, for hasmany relationships with a huge amount of data (e.g. 1 parent → hundreds of children), I only specify the belongsTo relationship on the child but not the hasMany one on the parent. Whenever I need to load the children, I query them manually anyhow. This has worked quite well for me so far.
