Filtering on hasMany by id without fetching object

I have a model with a hasMany collection, and I would like to determine whether or not an object exists in that collection, given the ID of an object.

var searchFor = "someid";
var filtered = parent.get('children').filter(function (item) { return item.get('child.id') == searchFor; });
var exists = filter.get('length') > 0;

However, this makes a remote request to the child endpoint with each iteration of the filter, which is unnecessary since it knows the IDs already. Is there any way to work around this?

2 Likes

Looks like the explicit comparison against child.id is unnecessary:

var exists = parent.get('children').findBy('child', child);

And that does it.

While that fixes my case, it is still strange that Ember Data forces the resolution of the entire object even if it’s only being used in a filter, and not being asked for as an instance. I would understand resolving the full object if I were requesting an instance of child, but not when I’m only interested in using the child object’s ID in a filter, a property that already exists in the cache.

Actually, this didn’t do it. Ember Data still fetches the entire object…