# How to get all currently loaded related records of a hasMany relationship without resolving it

**URL:** <https://discuss.emberjs.com/t/how-to-get-all-currently-loaded-related-records-of-a-hasmany-relationship-without-resolving-it/13472>\
**Category:** Ember Data\
**Created:** [August 16, 2017, 6:24am UTC](https://discuss.emberjs.com/t/how-to-get-all-currently-loaded-related-records-of-a-hasmany-relationship-without-resolving-it/13472 "2017-08-16T06:24:39Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [August 16, 2017, 6:24am UTC](https://discuss.emberjs.com/t/how-to-get-all-currently-loaded-related-records-of-a-hasmany-relationship-without-resolving-it/13472/1 "2017-08-16T06:24:39Z")

</div>

I have a large one-to-many relationship modeled by ember-data `hasMany`.

```auto
// models/parent.js
export default DS.Model.extend({
  children: DS.hasMany('child'), // may have hundreds of children
});

```

```auto
// models/child.js
export default DS.Model.extend({
  parent: DS.belongsTo('parent'),
});

```

Since a `parent` has so many `children`, I do not want to resolve the relationship all at once.

```auto
// Avoid this, since it resolves all the children at once
const childrenPromise = parent.get('children');

```

Instead, I’m implementing pagination. As far as I’ve seen, there is currently no built-in pagination for ember-data. So I’m implementing a custom solution, using the opaque `query()` adapter method and passing in the relevant paging information.

Here’s my question: **For a given `parent` record, I need access to an array of all `children` currently loaded in the client-side store** – without resolving the relationship. Furthermore, I need the array to update automatically as records in the store are changed, added, or removed.

Is there an easy way to do this? If not, what will it take to make it happen?

Could I use the [ds-references](https://www.emberjs.com/blog/2016/05/03/ember-data-2-5-released.html#toc_code-ds-references-code) functionality to achieve this without forcing the relationship to resolve? If so, is there a clean way to put the necessary code in the `parent` model, so I can simply query a property of the `parent` record and be given a live-updating array of related records?

---

<div class="post-metadata">

**Author:** ![jasonmit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jasonmit/32/182_2.png) [@jasonmit](https://discuss.emberjs.com/u/jasonmit)\
**Post date:** [August 16, 2017, 8:49am UTC](https://discuss.emberjs.com/t/how-to-get-all-currently-loaded-related-records-of-a-hasmany-relationship-without-resolving-it/13472/2 "2017-08-16T08:49:40Z")

</div>

`parent.hasMany('children').value() // <- synchronous, will not fetch`

> **[DS.HasManyReference - 2.14 - Ember API Documentation](https://api.emberjs.com/ember-data/2.14/classes/DS.HasManyReference/methods/value?anchor=value)**
>
> A HasManyReference is a low level API that allows users and addon author to perform meta-operations on a has-many relationship.

---

<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:** [August 16, 2017, 2:10pm UTC](https://discuss.emberjs.com/t/how-to-get-all-currently-loaded-related-records-of-a-hasmany-relationship-without-resolving-it/13472/3 "2017-08-16T14:10:28Z")

</div>

Thank you for your quick reply!

The documentation you linked to says the following:

> If the relationship is not loaded it will always return `null`.

What does it mean for the relationship to not be “loaded” in this context? I thought the whole idea of the `hasMany().value()` method is to get the relationship _without_ loading it.

If it means the method returns `null` when there are no records in the relationship, this could pose a problem. If no records have been loaded into the relationship, I need my model’s method to return _an empty array_ that will be populated asynchronously as records are added into the store.
