0

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?

4

1 回答 1

0

默认情况下,其余适配器仅在 belongsTo 端发送外键。您需要覆盖此钩子https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L169以在非嵌入式案例中添加 hasMany id出色地。

于 2013-06-03T22:15:39.037 回答