# Model belongs to more than one other models

**URL:** <https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392>\
**Category:** Ember Data\
**Created:** [July 15, 2015, 1:47pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392 "2015-07-15T13:47:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giorgos\_Karanikas](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@Giorgos\_Karanikas](https://discuss.emberjs.com/u/Giorgos_Karanikas)\
**Post date:** [July 15, 2015, 1:47pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/1 "2015-07-15T13:47:36Z")

</div>

I consumer an API that re-uses some models. What I want to do is re-use my ember model in several others using the hasMany and belongsTo.

Example code:

```
// Furniture model
export default DS.Model.extend({
  parent: DS.belongsTo('what goes here??'),
  color: DS.attr(),
  type: DS.attr()
});

// House model
export default DS.Model.extend({
  name: DS.attr(),
  furnitures: DS.hasMany('furniture')
});

// School model
export default DS.Model.extend({
  name: DS.attr(),
  furnitures: DS.hasMany('furniture')
});

```

Could someone please explain to me whether this is possible and how it is achieved?

---

<div class="post-metadata">

**Author:** ![kylecoberly](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kylecoberly/32/15828_2.png) [@kylecoberly](https://discuss.emberjs.com/u/kylecoberly)\
**Post date:** [July 15, 2015, 2:48pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/2 "2015-07-15T14:48:47Z")

</div>

You only need need to declare the relationship on both sides if you’ll need to be able to lookup records in both directions (eg. “Get the House record that this Furniture is a part of”). In your case, you probably just need the `hasMany`, since your House or School model will probably always be loaded before.

That said, if you DID want to it, it would look like this:

```auto
// Furniture model
export default DS.Model.extend({
  house: DS.belongsTo('house'),
  school: DS.belongsTo('school'),
  color: DS.attr(),
  type: DS.attr()
});

```

---

<div class="post-metadata">

**Author:** ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)\
**Post date:** [July 15, 2015, 4:07pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/3 "2015-07-15T16:07:01Z")

</div>

There’s actually a second option here with polymorphic relationships.

Here’s a good presentation on it: [Ember Data: Polymorphic Associations](http://lukegalea.github.io/ember_data_polymorphic_presentation/) (Haven’t vetted how up-to-date it is).

In your case, your models would look like:

```auto
// Furniture model
export default DS.Model.extend({
  parent: DS.belongsTo('house', {polymorphic: true}),
  color: DS.attr(),
  type: DS.attr()
});

// House Model
export default DS.Model.extend({
  name: DS.attr(),
  furnitures: DS.hasMany('furniture')
});

// School Model
import house from './house/model';
export default house.extend({
  // Any school specific model stuff here.
});

```

---

<div class="post-metadata">

**Author:** ![kylecoberly](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kylecoberly/32/15828_2.png) [@kylecoberly](https://discuss.emberjs.com/u/kylecoberly)\
**Post date:** [July 15, 2015, 4:14pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/4 "2015-07-15T16:14:23Z")

</div>

Nice call out on polymorphic relationships! I don’t think I’ve ever used a polymorphic belongsTo before. In this case, however, is School a type of House? It seems more likely that they would both be a type of Buidling. Eg:

```auto
// Furniture model
export default DS.Model.extend({
  furniture: DS.belongsTo('building', {polymorphic: true}),
  color: DS.attr(),
  type: DS.attr()
});

// Building Model
export default DS.Model.extend({
  name: DS.attr(),
  furnitures: DS.hasMany('furniture')
});

// House Model
import House from './buiding/model';
export default Building.extend({
  // Any house specific model stuff here.
});

// School Model
import House from './building/model';
export default Building.extend({
  // Any school specific model stuff here.
});

```

---

<div class="post-metadata">

**Author:** ![Giorgos\_Karanikas](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@Giorgos\_Karanikas](https://discuss.emberjs.com/u/Giorgos_Karanikas)\
**Post date:** [July 15, 2015, 4:30pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/5 "2015-07-15T16:30:07Z")

</div>

Thank you both for your responses. My models are not the same (school and house here are just a simplified example).

Althouth I guess having a common model which others extend can work fine. I will try this out as soon as I get to the office and let you know how it went.

---

<div class="post-metadata">

**Author:** ![Spencer\_Price](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/spencer_price/32/14878_2.png) [@Spencer\_Price](https://discuss.emberjs.com/u/Spencer_Price)\
**Post date:** [July 15, 2015, 5:44pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/6 "2015-07-15T17:44:27Z")

</div>

Yes, that’s probably how it should be done. 🙂

---

<div class="post-metadata">

**Author:** ![Giorgos\_Karanikas](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@Giorgos\_Karanikas](https://discuss.emberjs.com/u/Giorgos_Karanikas)\
**Post date:** [July 16, 2015, 6:53am UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/7 "2015-07-16T06:53:43Z")

</div>

The example from @kylecoberly works great. The only change is that there is no need to add `polymorphic: true`. Thank you guys again.

---

<div class="post-metadata">

**Author:** ![allthesignals](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/allthesignals/32/16089_2.png) [@allthesignals](https://discuss.emberjs.com/u/allthesignals)\
**Post date:** [October 4, 2017, 8:59pm UTC](https://discuss.emberjs.com/t/model-belongs-to-more-than-one-other-models/8392/8 "2017-10-04T20:59:37Z")

</div>

Hey folks,

I’m curious how you get this to work from both sides? I can’t seem to get the model reference to exist for both sides (only one).
