Can't consume json with relationship


#1

Hello,

I have a json server (in phoenix/elixir) and it generates this json:

{
sections: [
{
topics: [
{
title: "Botafogo melhor time",
subtitle: "quem concorda faz barulho",
id: 4,
content: "BOTAFOGO 10X0 NO FLAMENGO CTZ"
},
{
title: "Real vs Barça",
subtitle: "vai ser mt loko",
id: 2,
content: "acho que o benzema vai mete 10x0 lol"
},
{
title: "Real vs Barça",
subtitle: "vai ser mt loko",
id: 3,
content: "acho que o benzema vai mete 10x0 lol"
},
{
title: "anewtopic",
subtitle: "lolnicetopic",
id: 1,
content: "lolnicecontent"
}
],
name: "nice sectionlol",
id: 1,
description: "nice section"
},
{
topics: [ ],
name: "novatentativa",
id: 2,
description: "tentativa"
},
{
topics: [
{
title: "adoro esse forum",
subtitle: "nao acham",
id: 6,
content: "nao concordam???"
}
],
name: "novatentativa",
id: 3,
description: "tentativa"
},
{
topics: [
{
title: "Real madrid rula",
subtitle: "mas cr7 é lixo",
id: 5,
content: "vcs nao acham?? rsrsrs"
}
],
name: "novatentativa",
id: 4,
description: "tentativa"
},
{
topics: [ ],
name: "novatentativa",
id: 5,
description: "tentativa"
},
{
topics: [ ],
name: "novatentativa",
id: 6,
description: "tentativa"
},
{
topics: [
{
title: "eahseuhsaei",
subtitle: "hehaeheuae",
id: 7,
content: "ehuaihueae"
}
],
name: "Geralzão",
id: 7,
description: "descriassasae"
}
]
}

To explain: a section has topics, and a topic belongs to a section.

This is my topic model definition:

import DS from 'ember-data';
export default DS.Model.extend({
  section: belongsTo('section'),
});

And, my section model:

import DS from 'ember-data';
export default DS.Model.extend({
  topics: hasMany('topic'),
});

Error: sections.index Assertion Failed: Passing classes to store methods has been removed. Please pass a dasherized string instead of undefined Error: Assertion Failed: Passing classes to store methods has been removed. Please pass a dasherized string instead of undefined at new Error (native)

My router:

export default Ember.Route.extend({
  model() {
    return Ember.RSVP.hash({
      sections: this.store.findAll('section')
    });
  });
});

any tips? ;ṕ


#2

It looks like you have the topic records embedded in each section, you need to let Ember know about that, try adding a file called section.js in the serializers folder with this content, sorry for the formatting (not sure why that has stopped working).

import DS from ‘ember-data’; export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: { ‘topics’: { embedded: ‘always’ } } });

There’s some more info here http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html


#3

Thank you ricky, you are amazing! I solved my problem turning the nested “topics” an array of topic ids, but your approach looks way better.