has_many configuration for Ember-Data and Active Model Serializers with embedded IDs and sideloading

岁酱吖の 提交于 2019-12-05 07:42:58

For ember-data 1.0.0-beta.3 I ended up using this:

App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});

As explained here: Transition Guide

Works very nicely!

You can try to configure the right mapping in the adapter at the client side

DS.RESTAdapter.map('App.Post', { comments: { keyName: 'comment_ids' } });

I'm using active_model_serializers 0.6.0 and ember_data 11. I don't see behaviour you are reporting.

My serializer:

class CentreSerializer < ActiveModel::Serializer
  embed :ids

  attributes :id, :name
  has_many :rooms
end

The output of localhost:3000/centres/1.json

{
  centre: {
    id: 1,
    name: "Centre0",
    rooms: [
      1,
      2,
      3,
      4,
      5
    ]
  }
}

In my case the rails app is producing the correctly formed json before it even gets across to ember. You shouldn't have to resort to mapping on the client side.

It appears that this commit is responsible for the situation. When AMS was updated to serialize has_one associations as association_id (bringing AMS into compliance with ember-data), it was also modified to serialize belongs_to associations as association_ids.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!