Using Fixtures with Polymorphic Relationships


#1

I’ve been banging my head against the wall on this, and there are almost no examples of polymorphic relationships with the fixture adapter. Page has a polymorphic 1:1 relationship with a model called PageContent. PageContent has two subtypes (TextOnly and Video). I want to be able to able to do a findAll for “page” and get all of the content back. What am I doing wrong?

JSBin


#2

Answered on Stack Overflow. Check the revised JSBin.

Essentially, I was writing the fixture data incorrectly. Here are the working models and fixtures in case anyone else was as hard up as I was for a working example:

App.Page = DS.Model.extend({
  title: DS.attr("string"),
  pageContent: DS.belongsTo("pageContent", {async: true, polymorphic: true})
});

App.PageContent = DS.Model.extend({
  page: DS.belongsTo("page")
});

App.TextOnly = App.PageContent.extend({
  text: DS.attr("string")
});

App.Video = App.PageContent.extend({
  url: DS.attr("string")
});

App.Page.FIXTURES = [
  {
    id: 1,
    title: "Introduction",
    pageContent: 1,
    pageContentType: "textOnly"
  },{
    id: 2,
    title: "Summary",
    pageContent: 1,
    pageContentType: "Video"
  }
];

App.TextOnly.FIXTURES = [
  {
    id: 1,
    text: "Hey buddy",
    page: 1
  }
];

App.Video.FIXTURES = [
  {
    id: 1,
    url: "www.goodvideo.com",
    page: 2
  }
];

Happy polymorphing!