# Fetch relations automatically

**URL:** <https://discuss.emberjs.com/t/fetch-relations-automatically/13127>\
**Category:** Uncategorized\
**Created:** [June 16, 2017, 6:09am UTC](https://discuss.emberjs.com/t/fetch-relations-automatically/13127 "2017-06-16T06:09:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ioanszabo](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@ioanszabo](https://discuss.emberjs.com/u/ioanszabo)\
**Post date:** [June 16, 2017, 6:09am UTC](https://discuss.emberjs.com/t/fetch-relations-automatically/13127/1 "2017-06-16T06:09:15Z")

</div>

Hi, I have two related models ( **Notification** ):

```
import DS from 'ember-data';
import MF from 'ember-data-model-fragments';

export default DS.Model.extend({
    type: DS.attr('string'),
    status: DS.attr('string'),
    details: DS.attr(),
    createdAt: DS.attr('date'),
    updatedAt: DS.attr('date'),
    ...
    document: DS.belongsTo('document')
});

```

Here is the **Document** model:

```
        import DS from 'ember-data';
        import MF from 'ember-data-model-fragments';

export default DS.Model.extend({
    type: DS.attr('string'),
    category: DS.attr('string'),
    number: DS.attr('string'),
    valid: MF.fragment('doc-valid'),
   ....
    details: MF.fragment('doc-details'),
	user: DS.belongsTo('user'),
    vehicle: DS.belongsTo("vehicle"),
});

```

What I want to do is when them model is fetching notification I want to also fetch related documents by making **additional requests for each of them**.

I have tried { async: false }, but is not working.

E.g. `this.store.findAll('notification)` should result in notifications with documents. For each document a new request should be made automatically and then model should be ready only when all requests are ready.

Is possible?

---

<div class="post-metadata">

**Author:** ![nerdyworm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nerdyworm/32/3860_2.png) [@nerdyworm](https://discuss.emberjs.com/u/nerdyworm)\
**Post date:** [June 16, 2017, 4:02pm UTC](https://discuss.emberjs.com/t/fetch-relations-automatically/13127/2 "2017-06-16T16:02:35Z")

</div>

Something like this in your route should do the trick

```auto
model() {
  return this.store.findAll('notifcation').then(function(notifcations) {
    const promises = notifcations.map(function(n) {
      retrun n.get('document');
    });

    return Ember.RSVP.all(promises);
  })
}

```

Let me know if this works for you or if it’s not clear!

Cheers, Ben

---

<div class="post-metadata">

**Author:** ![ioanszabo](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@ioanszabo](https://discuss.emberjs.com/u/ioanszabo)\
**Post date:** [June 19, 2017, 5:14am UTC](https://discuss.emberjs.com/t/fetch-relations-automatically/13127/3 "2017-06-19T05:14:19Z")

</div>

Hi, This works, but I want my model to be an array of notifications, in this case I suppose it will be an array of documents.

---

<div class="post-metadata">

**Author:** ![nerdyworm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nerdyworm/32/3860_2.png) [@nerdyworm](https://discuss.emberjs.com/u/nerdyworm)\
**Post date:** [June 19, 2017, 1:44pm UTC](https://discuss.emberjs.com/t/fetch-relations-automatically/13127/4 "2017-06-19T13:44:02Z")

</div>

```auto
return Ember.RSVP.all(promises).then(function() {
  return notifcations;
});

```

😉

---

<div class="post-metadata">

**Author:** ![ioanszabo](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@ioanszabo](https://discuss.emberjs.com/u/ioanszabo)\
**Post date:** [June 20, 2017, 5:04pm UTC](https://discuss.emberjs.com/t/fetch-relations-automatically/13127/5 "2017-06-20T17:04:45Z")

</div>

Thanks, this will do.
