[英]Active Model Serializer remove relationship member when using JSON API
我正在将最新的AMS v0.10.0.rc3
与JSON API适配器一起使用。
到目前为止,它运行良好,并且添加了一些我想更改的有用的约定。
例如,假设我有一个Post序列化程序和Comment序列化程序,如下所示:
class Post < ActiveModel::Serializer
attributes :id, :title
has_many :comments
end
class Comment < ActiveModel::Serializer
attributes :id, :comment
belongs_to :post
end
然后,如果我请求例如/posts/1
,我得到以下内容
{
"data": {
"id": "1",
"type": "posts",
"attributes": {
"title": "My awesome title",
},
"relationships": {
"comments": {
"data": [
{
"id": "1",
"type": "comments"
},
{
"id": "2",
"type": "comments"
}
]
}
}
}
}
请注意, relationships
成员如何出现,并根据规范使用MAY标记为可选成员。 这是一个很好的约定,我需要重写一些时间。
所以我的问题是:
relationship
成员? (如果我错过了一些细节,请发表评论,我将更新问题。)
当前,最好的选择是定义两个序列化器,一个具有关联,一个不带关联,并在渲染调用中指定要使用的序列化器( render json: posts, each_serializer: PostWithoutAssociationsSerializer
)。
正在进行有关将include_if
选项添加到关联和属性的讨论,如果将其合并,则可以利用该选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.