Remove id of deleted record on many to many relationship


#1

Hello! I’m having a small problem. I have two models: Story and Category, which are related many to many:

// app/models/story.js
export default DS.Model.extend({
  title: DS.attr('string'),
  text_content: DS.attr('string'),
  excerpt: DS.attr('string'),
  creation_date: DS.attr('date'),
  external_id: DS.attr('string'),
  categories: DS.hasMany('category', { async: true }),
})

// app/models/category.js
export default DS.Model.extend({
  name: DS.attr('string'),
  iptc: DS.attr('string'),
  stories: DS.hasMany('story', { async: true }),
  original_name: DS.attr('string')
})

Now I’m trying to delete them in this function:

deleteCategory: function(category) {
  category.deleteRecord();
  category.save();
}

However, this deletes the category but the stories that had its ID assigned still have it, but since we deleted it, Ember Data can’t find it, resulting in TypeError: Cannot read property 'stories' of undefined. What’s the most appropriate way to delete a many-to-many record and update the related records accordingly? Thanks!