首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails序列化- fast_jsonapi / active_model_serializers

Rails序列化- fast_jsonapi / active_model_serializers
EN

Stack Overflow用户
提问于 2019-12-25 05:19:26
回答 3查看 1.1K关注 0票数 0

我在使用fast_jsonapi / active_model_serializers构建应用程序接口时有点迷茫。我有基本的下来,但似乎卡在一个自定义的解决方案。

我将此作为序列化程序:

代码语言:javascript
复制
class AreaSerializer
  include FastJsonapi::ObjectSerializer
  attributes :id, :name, :cost_center, :notes
  has_many :children
end

在我的区域模型中,我有:

代码语言:javascript
复制
  has_many :children, -> { Area.where(ancestry: id) }

我的控制器看起来像:

代码语言:javascript
复制
class Api::V1::AreasController < ApiController

  def index
    render json: AreaSerializer.new(Area.root).serialized_json
  end

end

区域嵌套在具有祖先gem的层次结构中。输出为:

代码语言:javascript
复制
{
"data": [{
    "id": "1",
    "type": "area",
    "attributes": {
        "id": 1,
        "name": "Calgary",
        "cost_center": "123456",
        "notes": ""
    },
    "relationships": {
        "children": {
            "data": [{
                "id": "3",
                "type": "child"
            }]
        }
    }
}, {
    "id": "2",
    "type": "area",
    "attributes": {
        "id": 2,
        "name": "Edmonton",
        "cost_center": "78946",
        "notes": ""
    },
    "relationships": {
        "children": {
            "data": []
        }
    }
}]

}

我正在寻找一个这样的输出:

代码语言:javascript
复制
{
"data": [{
    "id": "1",
    "type": "area",
    "attributes": {
        "id": 1,
        "name": "Calgary",
        "cost_center": "123456",
        "notes": ""
    },
    "relationships": {
        "areas": {
            "data": [{
                "id": "3",
                "type": "area",
                "attributes": {
                    "id": 3,
                    "name": "Child Area",
                    "cost_center": "123456",
                    "notes": ""
                }
            }]
        }
    }
}, {
    "id": "2",
    "type": "area",
    "attributes": {
        "id": 2,
        "name": "Edmonton",
        "cost_center": "78946",
        "notes": ""
    }
}]

}

想法是嵌套的关系显示细节等。

EN

回答 3

Stack Overflow用户

发布于 2020-03-08 18:23:26

我刚刚开始在我的rails项目中使用fast_jsonapi

fast_jsonapi遵循JSON:API spec which you can see here.

因此,您将无法使用关系助手函数(:has_many、:belongs_to)来实现您想要的输出,即在relationships键中嵌套area的属性。

代码语言:javascript
复制
"relationships": {
        "areas": {
            "data": [{
                "id": "3",
                "type": "area",
                "attributes": { // nesting attributes of area
                    "id": 3,
                    "name": "Child Area",
                    "cost_center": "123456",
                    "notes": ""
                }
            }]
        }
    }

JSON:API指定:

为了减少

请求的数量,服务器可以允许包含相关资源以及所请求的主资源的响应。这样的响应被称为“复合文档”。

因此,您将在一个名为included的键中拥有area的属性。

为了在响应中获得included密钥,您需要在控制器中向序列化程序提供options散列。

我不太理解您的模型Area关系,但假设一个Area有许多SubArea

代码语言:javascript
复制
class Api::V1::AreasController < ApiController

  def index
    // serializer options
    options = {
      include: [:sub_area],
      collection: true
    }
    render json: AreaSerializer.new(Area.root, options).serialized_json
  end

end
票数 0
EN

Stack Overflow用户

发布于 2020-03-08 23:07:36

您不能在fast_jsonapi中使用关联。获取嵌套格式的响应。您需要添加方法,并且需要创建另一个序列化程序。

代码语言:javascript
复制
class AreaSerializer
  include FastJsonapi::ObjectSerializer

  set_type 'Area'

  attributes :id, :name, :cost_center, :notes

 attribute :childrens do |area, params|
     ChildrenSerializer.new(area.childrens, {params: 
     params})
  end
end

class ChildrenSerilizer
   include FastJsonapi::ObjectSerializer
    set_type 'Area'
    attributes :id, :name ... 
end
票数 0
EN

Stack Overflow用户

发布于 2021-03-31 12:24:52

我开始使用上面列出的技术,但最终使用了派生和重写jsonapi-serializer gem,因此它允许嵌套(最多4层深),并取消了拥有relationshipsattributes键的概念。我还感到沮丧的是,它只输出ID和类型键,其中的类型在大多数情况下是多余的,因为作为示例,对象通常在数组中保持相同的类,并且人们很少使用真正的多态性(尽管它仍然支持这一点,并输出ID/类型用于多态关系)。

我重写的最好的部分可能是,它允许通过新的fields输入在json键树中的任何位置进行确定性字段选择。

https://github.com/rubesMN/jsonapi-serializer

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59473475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档