Many to many relationship issues


#1

Hello,

I have posts with categories. On post can have different categories and one category can belong to different posts.

My models are as follow:

post.js

export default DS.Model.extend({
  slug: DS.attr(),
  title: DS.attr(),
  teaser: DS.attr(),
  body: DS.attr(),
  media: DS.belongsTo('media', {async: true}),
  date: DS.attr("date"),
  author: DS.belongsTo('user', {async: true}),
  categories: DS.hasMany('category', {async: true}),
  featured_position: DS.attr(),
});

category.js

export default DS.Model.extend({
  description: DS.attr(),
  name: DS.attr(),
  slug: DS.attr(),
  taxonomy: DS.attr(),
  parent: DS.attr(),
  posts: DS.hasMany('post', {async: true}),
});

Next I bring all the posts in my model:

blog.js

model(){
    return this.store.findAll('post');
  },

After that I loop through all the posts in my template:

blog.hbs

{{#each model as |post|}}
	{{#each category in post.categories}}
		Category: {{category.name}}
	{{/each}}
	{{post.author.name}}
	{{post.title}}
{{/each}}

So when I do this, all of the other attributes get printed alright, including the attributes with a belongsTo, but the categories are blank.

Additionally if I look at the Ember Inspector the category data is being fetched from the backend. The problem is that the data is not being referenced between the post and the category.

Is there something else that I have to take into account when doing a many to many relationship in Ember?


#2

Answered on reddit.


#3

Yes, thanks for your help.