EnriMR
EnriMR

Reputation: 3970

How to filter a model by belongsTo relationship property in EmberJS

I'm trying to filter a findAll query to get only the elements without relationship.

My model is created by two classes:

Element

export default Model.extend({
    position: attr('number'),
    name: attr('string'),
    shared: attr('boolean', { defaultValue: false }),
    stack: belongsTo('stack')
});

 Stack

export default Model.extend({
  position: attr('number'),
  name: attr('string'),
  shared: attr('boolean', { defaultValue: false }),
  element: hasMany('element')
});

I have tried to filter by property stack filterBy('stack', null) and filterBy('stack.content', null) but they didn't run.

Any idea and help of how to filter by relationships?

Upvotes: 1

Views: 470

Answers (1)

EnriMR
EnriMR

Reputation: 3970

After looking for information, the solution is this:

var elements = new Ember.RSVP.Promise(function(resolve) {
  that.store.findAll('element').then(function(elements) {
    resolve(elements.filterBy('stack.content', null));
  })});

If you need to get the element that is in the relationship, you need to access to content attribute in order to be able to compare the value.

Upvotes: 1

Related Questions