filterBy using a hasMany relationship


#1

Hi,

My chatroom model includes a hasMany comments and my user model also includes hasMany comments.

With my chatroom model I’m trying to find my comments for the chatroom. using:

var mySelf = this;
return this.chatroomModel.get('comments').then((myComments) => {
     var commentList = myComments.filterBy('user', mySelf.get('user').get('currentUser'));
     console.log("Array: ", commentList );
});

If I filter by any comment property it works but filtering by user does not. Any ideas as to why?


#2

The user property on the comment model probably returns a promise (if you’re using async relationships like you should be). Since filterBy uses the triple equals for equality (===) and the promise objects are not the same exact JS objects, they are all getting filtered out.


#3

If this is the case, check out the References API. See this blog post: http://emberjs.com/blog/2016/05/03/ember-data-2-5-released.html