0
votes

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

used libs

Rails 3.2.13
gem act_as_taggable_on
ember RC2
ember-data (rev: 12), RESTfulAdapter

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.

JSON sent to rails

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

Using a join model didn't help and it feels like both are bad approach 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?

If ember-data is not supporting it, how could my issue be solved?

1
Can this be solved by overriding the RESTAdapter for a tag model?Mike

1 Answers

0
votes

By default the rest adapter only sends foreign keys on the belongsTo side. You would need to overwrite this hook https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L169 to add the hasMany ids in the non-embedded case as well.