Loading all the associations in a set in a single request


#1

How does one load hasMany associations for an entire set at once?

Given User who hasMany Memberships, if the User json payload includes membership_ids for each… It’s trivial to grab a single user and get memberships, which makes a request with ids=1,2,3,4,etc

But how does once combine all the membership requests for the entire set of users to request the memberships in a single request?

I was hoping for something like this.get(‘users.@each.memberships’).then(…) but alas, that’s no good.

Thanks in advance!


#2

I think this is the default behavior in the latest ember-data (1.0-beta3 as of writing), unless you specify {async: true} in your hasMany relationship. Your users payload might look something like this:

{
  "users": [{...}, {...}],
  "memberships": [{...}, {...}]
}

#3

Thanks daliwali. I’m trying to avoid loading both the users and memberships in a single query because not every place in the app wants them both.

If I include just the ids for memberships, and don’t mark the request as async I get:

You looked up the ‘memberships’ relationship on ‘…’ but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.hasMany({ async: true }))

Hence my need for something like:

this.get('users.@each.memberships').then(...)