# Polymorphic Has Many Relationship

**URL:** <https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319>\
**Category:** Ember Data\
**Created:** [February 14, 2015, 4:09am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319 "2015-02-14T04:09:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakerac4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bakerac4/32/10593_2.png) [@bakerac4](https://discuss.emberjs.com/u/bakerac4)\
**Post date:** [February 14, 2015, 4:09am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/1 "2015-02-14T04:09:20Z")

</div>

I just stumbled on to Ember Data Polymorphic Relationships. I think they would be useful for what I am working on. Here is my main model. This model defines a has many relationship with topics thats both async and polymorphic.

```
    App.Source = DS.Model.extend({
        name: DS.attr('string'),
        type: DS.attr('string'),
        locationType: DS.attr('string'),
        locationSpecific: DS.attr('boolean'),
        primary: DS.attr('boolean'),
        published: DS.attr('boolean'),
        sort: DS.attr('number'),
        topics: DS.hasMany('topic', {
            async: true,
            polymorphic: true
        })
    });

```

Next we have the Topics with a Base class being ‘Topic’

```
    App.Topic = DS.Model.extend({
        name: DS.attr('string'),
        sort: DS.attr('number'),
        source: DS.belongsTo('source')
    });
    
    App.RegTopic = App.Topic.extend({
        test: DS.attr('number', {
            defaultValue: 8
        }),
        notes: DS.hasMany('notes', {
            async: true
        })
    });
    
    App.SummaryTopic = App.Topic.extend({
        number: DS.attr('number', {
            defaultValue: 9
        })
    });

```

and here is how I call to get the topics

```
App.TopicsRoute = Ember.Route.extend({
     model: function() {
           return this.modelFor('source').get('topics');
     }
});

```

When I list the sources, I get a list of the following objects back

```
 {
         id: 1
         name: "test"
         type: "testType"
         locationType: "international"
         locationSpecific: "true"
         primary: true
         published: true
         sort: 1
         links: {
                 topics: "/topics?sourceId=1"
         }
    }

```

then my topic call gets objects like these back

```
{
   id: 4
   sourceId: 1
   name: Topic 4
   type: "regTopic"
   sort: 1
}

```

Am I missing something? Can you not use polymorphic relationships with the ‘links’ object?

From my understanding of the Polymorphic Relationship, when I make the call to /topics?sourceId=1 its should be essentially loading 2 different topics in one call so then I can display the regTopic and summaryTopic on the same page but in seperate lists and keep them as seperate objects?

---

<div class="post-metadata">

**Author:** ![gregone](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gregone/32/15763_2.png) [@gregone](https://discuss.emberjs.com/u/gregone)\
**Post date:** [February 14, 2015, 4:58am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/2 "2015-02-14T04:58:50Z")

</div>

Shouldn’t your model include this:

```auto
links: DS.hasMany('topic', {
  async: true,
  polymorphic: true
})

```

instead of `topics` ?

---

<div class="post-metadata">

**Author:** ![bakerac4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bakerac4/32/10593_2.png) [@bakerac4](https://discuss.emberjs.com/u/bakerac4)\
**Post date:** [February 14, 2015, 1:43pm UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/3 "2015-02-14T13:43:19Z")

</div>

I don’t believe so. That links hash is a way to lazy load data without passing ID’s. Ember looks for that links hash then connects topics with the topics in my model. And that part does work, when I do

```
 return this.modelFor('source').get('topics');

```

It makes the call to go get the topics using the URL passed down (/topics?sourceId=1)

> <https://stackoverflow.com/questions/18812730/ember-data-lazy-load-association-with-links-attribute>

---

<div class="post-metadata">

**Author:** ![bakerac4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bakerac4/32/10593_2.png) [@bakerac4](https://discuss.emberjs.com/u/bakerac4)\
**Post date:** [February 15, 2015, 5:24pm UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/4 "2015-02-15T17:24:43Z")

</div>

Well, I guess I should really re ask this questions as ‘Can you use polymorphic relationships and the links hash’?

---

<div class="post-metadata">

**Author:** ![bmac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bmac/32/15389_2.png) [@bmac](https://discuss.emberjs.com/u/bmac)\
**Post date:** [February 16, 2015, 12:13am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/5 "2015-02-16T00:13:57Z")

</div>

@bakerac4 I believe polymorphic relationships are supported with the links hash. All the code you’ve shown so far looks correct. Could you describe what issues you are seeing that make it seem like the polymorphic relationships aren’t working?

---

<div class="post-metadata">

**Author:** ![bakerac4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bakerac4/32/10593_2.png) [@bakerac4](https://discuss.emberjs.com/u/bakerac4)\
**Post date:** [February 16, 2015, 12:38am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/6 "2015-02-16T00:38:29Z")

</div>

Hey @bmac thanks for the reply!

Here is what I see in my ArrayController

Here is the list of content

I would expect this list to have App.SummaryTopic and & App.RegTopic ?

Also in the console I see ‘WARNING: The payload for ‘App.Topic’ contains these unknown keys: [type]. Make sure they’ve been defined in your model.’ which if it was working correctly It shouldn’t be complaining about.

Some other possibly pertinent information: Im using Ember v1.9.0 and ember-data v1.0.0~beta.12 and the the DS.RESTAdapter

---

<div class="post-metadata">

**Author:** ![bmac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bmac/32/15389_2.png) [@bmac](https://discuss.emberjs.com/u/bmac)\
**Post date:** [February 16, 2015, 2:35am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/7 "2015-02-16T02:35:50Z")

</div>

I’ve seen that type warning happen before with embedded polymorphic relationships. What happens if you add a type attribute to the `Topic` model?

---

<div class="post-metadata">

**Author:** ![bakerac4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bakerac4/32/10593_2.png) [@bakerac4](https://discuss.emberjs.com/u/bakerac4)\
**Post date:** [February 16, 2015, 3:12am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/8 "2015-02-16T03:12:31Z")

</div>

The only thing that changes is that there is now a type called ‘summaryTopic’ or ‘regTopic’ in my App.Topic models.

Also I should point out that the properties defined in the models that inherit App.Topic never show up. (‘test’ or ‘number’)

---

<div class="post-metadata">

**Author:** ![bakerac4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bakerac4/32/10593_2.png) [@bakerac4](https://discuss.emberjs.com/u/bakerac4)\
**Post date:** [February 16, 2015, 3:41am UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/9 "2015-02-16T03:41:14Z")

</div>

Here is a js-bin that I’m trying to get working. Could you take a look? [JS Bin - Collaborative JavaScript Debugging](http://emberjs.jsbin.com/medojitibo/1/edit?html,js,console,output) . It fails on trying to get the topics for the source using the links method with polymorphic relationships.

Either that or you don’t happen to know of or have a working js bin?

---

<div class="post-metadata">

**Author:** ![bmac](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bmac/32/15389_2.png) [@bmac](https://discuss.emberjs.com/u/bmac)\
**Post date:** [February 18, 2015, 9:25pm UTC](https://discuss.emberjs.com/t/polymorphic-has-many-relationship/7319/10 "2015-02-18T21:25:18Z")

</div>

Sorry for the delay @bakerac4. I worked with that jsbin you provided and I think I was able to get it working check out my updates here: [JS Bin - Collaborative JavaScript Debugging](http://emberjs.jsbin.com/litoda/1/edit?html,css,js,output)

It looks like async polymorphic relationships need a `type` parameter on the model. Also attempting to get the `topics` relationship before the `source` model was loaded seemed to be causing some issues so I moved it into the then callback.
