Quantcast
Viewing all articles
Browse latest Browse all 4828

Best practice modelling polymorph relationship

@mkconn wrote:

I've searched here and also the net, but I have to admit, that I did not really understand how to do this...

I have some models which relate to each other:

My problem is how to setup the relations in agenda, event, and location... (same would go for location - talk - event, but this would be solved when the agenda stuff will become clear...).

To explain it a bit (if not clear from the little scetch):

An agenda can either belong to an event or a location. Location and event can have many of agendas.

Do I have to declare something like:

//agenda
export default DS.Model.extend({
    event: DS.belongsTo('event', {polymorphic: true}),
    location: DS.belongsTo('location', {polymorphic: true})
});

//event
export default DS.Model.extend({
    agendas: DS.hasMany('agenda')
});

//location
export default DS.Model.extend({
    agendas: DS.hasMany('agenda')
});

If its like this - how would the json have to look like (I'm using JSON API -http://jsonapi.org ) so ember-data will build the relations correct? I think there is no documentation on this (at least I didn't find any). Or is using polymorphs the wrong approach and instead inverses are the right solution? Sorry if that's a dumb question but I really don't get it...

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles