user215674
user215674

Reputation: 167

Ember embedded object repeated

In Ember, I have a model, that has child objects that are embedded, both as belongsTo and hasMany. I'm using Ember Data backing onto a Rails API, and using Active Model Serializer. Set up of the object is -

Application.Release = DS.Model.extend({
    title: DS.attr(),
    ...
    label: DS.belongsTo('label'),
    artists: DS.hasMany('artist')
});

Application.ReleaseSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, { 
    attrs: { 
        label: { embedded: 'always' },
        artists: { embedded: 'always' }
    } 
});

Application.Label = DS.Model.extend({
    name: DS.attr(),
    release: DS.belongsTo('release')
});

Application.Artist = DS.Model.extend({
    name: DS.attr(),
    release: DS.belongsTo('release')
});

That is all find and working across the board.

My issue comes when on the releases index, listing all the releases. When an artist or label is used on more than one release, it is only included on final of all the releases, missing from the previous objects. For instance -

Title          Artist        Label

Release 1      Artist 1      Label 1
Release 2                  
Release 3      Artist 2      Label 2

The JSON for Release 2 contains the Artist 2 and Label 2 as expected, but are missing from the Ember objects.

Am I missing something crucial about how Ember thinks about these objects? How can I ensure that they are included on each object as is reflected in the JSON?

Upvotes: 0

Views: 52

Answers (1)

Gennady Dogaev
Gennady Dogaev

Reputation: 5991

Its because your artist can't have many releases. You should change

Application.Artist = DS.Model.extend({
    name: DS.attr(),
    release: DS.belongsTo('release')
});

to

Application.Artist = DS.Model.extend({
    name: DS.attr(),
    release: DS.hasMany('release')
});

The same with label

Upvotes: 1

Related Questions