ruby-on-rails – 具有嵌入式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();

期望将评论关联序列化为注释:[…]不带_ids后缀(参见the relationships sub-section of the REST adapter section of the Ember.js guide).

我尝试了以下作为解决方法

class PostSerializer < ActiveModel::Serializer
  attribute :comments
  def comments
    object.comment_ids
  end
end

它有效,但添加了embed:ids,:include =>因为AMS不知道它是一个关联,所以为了启用侧载现在什么都不做.

编辑:我正在使用active_model_serializers(0.6.0)gem和Ember-Data修订版11

解决方法

对于ember-data 1.0.0-beta.3,我最终使用了这个:
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});

如下所述:Transition Guide

工作得非常好!

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...