1
votes

I'm really struggling to understand how polymorphic relationships worm in Ember Data (Beta 11) and cannot find any update information on how to set them up and what is expected in the JSON payload. I'm trying to create a feed of items (think facebook feed) where you have different types of items in the feed. My modeling looks something like the following.

App.Feedable = DS.Model.extend({
 activities: DS.hasMany('activity')
});

App.Activity = DS.Model.extend({
 feedable: DS.belongsTo('feedable', { polymorphic: true, async: false })
});

App.MemberLikeShare = DS.Model.extend({
 status: DS.attr('string')
});

App.PhotoShare = DS.Model.extend({
 status: DS.attr('string'),
 photo: DS.attr('string')
});

When I do a fetch at /activities I send back JSON that looks like the following:

{
  activities: [
    {
      id: 1,
      feedable: {  id: 1, type: 'memberLikeShare' }
    },
    {
     id: 4,
     feedable: { id: 4, type: 'memberLikeShare' }
    },
    {
      id: 5,
      feedable: { id: 5, type: 'photoShare' }
    }
  ],

  member_like_shares: [
    {
      id: 1,
      status: 'Foo'
    },
    {
     id: 4,
     status: 'Bar'
    }
  ],
  photo_shares: [
    {id: 5, photo: 'example.jpg'}
  ]
}

When this runs I get an error like:

You can only add a 'feedable' record to this relationship Error: Assertion Failed: You can only add a 'feedable' record to this relationship

I'm assuming my relationships are wrong or I'm sending the wrong JSON?

1

1 Answers

0
votes

polymorphic relationships should extend the base type.

App.Feedable = DS.Model.extend({
 activities: DS.hasMany('activity')
});

App.MemberLikeShare = App.Feedable.extend({
 status: DS.attr('string')
});

App.PhotoShare = App.Feedable.extend({
 status: DS.attr('string'),
 photo: DS.attr('string')
});

I'd also expect them to define the activities on them.

 member_like_shares: [
    {
      id: 1,
      status: 'Foo',
      activites: [1,2,3,4]
    },
    {
     id: 4,
     status: 'Bar',
      activites: [1,2,3,4]
    }
  ],
  photo_shares: [
    {
      id: 5, 
      photo: 'example.jpg',
      activites: [1,2,3,4]
    }
  ]