具有嵌入式ID和侧载的Ember-Data和Active Model Serializer的has_many配置
我知道Ember-Data应该与设计的Active Model Serializer兼容,但它们似乎与序列化与嵌入式ID的has_many
关系不合时宜。
例如,序列化器
class PostSerializer < ActiveModel::Serializer embed :ids has_many :comments end
生成JSON
{ "post": { "comment_ids": [...] } }
但是Ember Data中的默认配置,
App.Post = DS.Model.extend({ DS.hasMany('App.Comment'), }); App.Comment = DS.Model.extend();
期望注释关联序列化为comments: [...]
没有_ids
后缀(请参阅Ember.js指南的REST适配器部分的关系子部分 )。
我尝试了以下作为解决方法:
class PostSerializer < ActiveModel::Serializer attribute :comments def comments object.comment_ids end end
它可以工作,但添加embed :ids, :include => true
以便启用侧载现在什么都不做,因为AMS不知道它是一个关联。
编辑:我正在使用active_model_serializers (0.6.0)
gem和Ember-Data修订版11
对于ember-data 1.0.0-beta.3,我最终使用了这个:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});
如此处所述: 过渡指南
工作得非常好!
您可以尝试在客户端的适配器中配置正确的映射
DS.RESTAdapter.map('App.Post', { comments: { keyName: 'comment_ids' } });
我正在使用active_model_serializers 0.6.0和ember_data 11.我没有看到你报告的行为。
我的序列化器:
class CentreSerializer < ActiveModel::Serializer embed :ids attributes :id, :name has_many :rooms end
localhost的输出:3000 / centers / 1.json
{ centre: { id: 1, name: "Centre0", rooms: [ 1, 2, 3, 4, 5 ] } }
在我的情况下,rails应用程序正在生成正确形成的json,甚至在它进入ember之前。 您不必在客户端使用映射。
看来这个提交是造成这种情况的原因。 当更新AMS以将has_one
关联序列化为association_id
(使AMS符合ember-data)时,还修改了将belongs_to
关联序列化为association_ids
。
- Active Model Serializers:nil的未定义方法`url_for’:NilClass
- 活动模型序列化程序belongs_to
- <NoMethodError:#Record :: ActiveRecord_Relation的未定义方法`read_attribute_for_serialization':
- 使用active_model_serializers序列化权限(例如CanCan)
- ActiveModel :: Serializers Gem – Versioned API命名空间问题
- 如何在active_model_serializers中执行预先加载
- 如何使用active_model_serializers为嵌套对象选择序列化程序
- Rails ActiveModelSerializer将两个相同类型模型的列表组合成一个序列化响应,具有不同的名称
- Url Helper在ActiveModelSerializer 0.10.0中?