gpt4 book ai didi

ruby-on-rails - 具有嵌入式ID和侧载的Ember-Data和Active Model序列化程序的has_many配置

转载 作者:行者123 更新时间:2023-12-04 05:39:50 25 4
gpt4 key购买 nike

我知道Ember-Data在设计上应该与Active Model序列化程序兼容,但是它们似乎在序列化带有嵌入式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后缀(请参阅 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 => true以启用侧面加载并没有任何作用,因为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

效果很好!

关于ruby-on-rails - 具有嵌入式ID和侧载的Ember-Data和Active Model序列化程序的has_many配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14633432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com