# How to use hasMany on both sides (habtm)

**URL:** <https://discuss.emberjs.com/t/how-to-use-hasmany-on-both-sides-habtm/1441>\
**Category:** Ember Data\
**Created:** [June 8, 2013, 1:11pm UTC](https://discuss.emberjs.com/t/how-to-use-hasmany-on-both-sides-habtm/1441 "2013-06-08T13:11:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mszoernyi](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@mszoernyi](https://discuss.emberjs.com/u/mszoernyi)\
**Post date:** [June 8, 2013, 1:11pm UTC](https://discuss.emberjs.com/t/how-to-use-hasmany-on-both-sides-habtm/1441/1 "2013-06-08T13:11:06Z")

</div>

I’m trying to learn new thing in ember every day and I’m stuck with ember-data and hasMany associations.

**issue**

I want to add a tagging feature which means “add and remove tags to posts”. So I need a post model which `hasMany` tags. But the same tag can be used on different posts so a tag `hasMany` posts as well.

Post.js

```
App.Post = Ember.Model.extend({
  title: DS.attr('string')
  body: DS.attr('string')
  tags: DS.hasMany('App.Tag')
})

```

Tag.js

```
App.Tag = Ember.Model.extend({
  name: DS.attr('string')
  posts: DS.hasMany('App.Post')
})

```

After trying to add a new tag to a post and commit it’s changes, my json payload misses always the post\_id.

```
p = Post.find(1)
p.get('tags').createRecord({name: "test tag name"})
p.get('store').commit()

```

JSON sent to rails

```
{"tag"=>{"name"=>"test tag name"}}

```

Using a join model didn’t help and it feels like both are bad approaches at all.

I’ve read through the test specs of ember-data but there was no habtm or similar test case, so maybe it’s just not supported.

**Question**

What is the ember way to define `hasMany` and `belongsTo` associations on both sides or is there a better way in general to solve my problem?

I tried to understand how ember-data is generating these payloads and tried to override `addHasMany` but it’s more guessing how it should work instead of understanding it. If ember-data doesn’t support habtm, I would be happy to solve it via pullrequest but I need some help first 🙂

---

<div class="post-metadata">

**Author:** ![toddwildey](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@toddwildey](https://discuss.emberjs.com/u/toddwildey)\
**Post date:** [August 27, 2013, 12:38am UTC](https://discuss.emberjs.com/t/how-to-use-hasmany-on-both-sides-habtm/1441/3 "2013-08-27T00:38:52Z")

</div>

[This](http://dbushell.com/2013/04/25/ember-data-and-mongodb/) seems to have solved my issues regarding my HABTM relationship problems. Hopefully it’ll resolve your problems also.

---

<div class="post-metadata">

**Author:** ![ginolon](https://avatars.discourse-cdn.com/v4/letter/g/b487fb/32.png) [@ginolon](https://discuss.emberjs.com/u/ginolon)\
**Post date:** [May 30, 2017, 11:48am UTC](https://discuss.emberjs.com/t/how-to-use-hasmany-on-both-sides-habtm/1441/4 "2017-05-30T11:48:38Z")

</div>

Any hint for this similar problem? [https://stackoverflow.com/questions/44259423/rails-and-ember-with-acts-as-taggable-on-how-to-handle-relationships-in-jsonapi](https://stackoverflow.com/questions/44259423/rails-and-ember-with-acts-as-taggable-on-how-to-handle-relationships-in-jsonapi)
