草庐IT

ruby-on-rails - rails : Serializing deeply nested associations with active_model_serializers

coder 2023-04-30 原文

我正在使用 Rails 4.2.1active_model_serializers 0.10.0.rc2

我是 API 新手并选择了 active_model_serializers,因为它似乎正在成为 rails 的标准(尽管我不反对使用 RABL 或其他序列化程序)

我遇到的问题是我似乎无法在多级关系中包含各种属性。例如,我有:

项目

class ProjectSerializer < ActiveModel::Serializer
  attributes                      :id, 
                                  :name,
                                  :updated_at

  has_many                        :estimates, include_nested_associations: true

end

估计

class EstimateSerializer < ActiveModel::Serializer
  attributes                      :id, 
                                  :name, 
                                  :release_version, 
                                  :exchange_rate, 
                                  :updated_at,

                                  :project_id, 
                                  :project_code_id, 
                                  :tax_type_id 

  belongs_to                      :project
  belongs_to                      :project_code
  belongs_to                      :tax_type

  has_many                        :proposals

end

提案

class ProposalSerializer < ActiveModel::Serializer
  attributes                      :id, 
                                  :name, 
                                  :updated_at,

                                  :estimate_id

  belongs_to                      :estimate
end

当我点击 /projects/1 时,上面会产生:

{
  "id": 1,
  "name": "123 Park Ave.",
  "updated_at": "2015-08-09T02:36:23.950Z",
  "estimates": [
    {
      "id": 1,
      "name": "E1",
      "release_version": "v1.0",
      "exchange_rate": "0.0",
      "updated_at": "2015-08-12T04:23:38.183Z",
      "project_id": 1,
      "project_code_id": 8,
      "tax_type_id": 1
    }
  ]
}

但是,我希望它产生的是:

{
  "id": 1,
  "name": "123 Park Ave.",
  "updated_at": "2015-08-09T02:36:23.950Z",
  "estimates": [
    {
      "id": 1,
      "name": "E1",
      "release_version": "v1.0",
      "exchange_rate": "0.0",
      "updated_at": "2015-08-12T04:23:38.183Z",
      "project": { 
        "id": 1,
        "name": "123 Park Ave."
      },
      "project_code": {
        "id": 8,
        "valuation": 30
      },
      "tax_type": {
        "id": 1,
        "name": "no-tax"
      },
      "proposals": [
        {
          "id": 1,
          "name": "P1",
          "updated_at": "2015-08-12T04:23:38.183Z"
        },
        {
          "id": 2,
          "name": "P2",
          "updated_at": "2015-10-12T04:23:38.183Z"
        }
      ]
    }
  ]
}

理想情况下,我还希望能够指定每个序列化程序中包含哪些属性、关联以及这些关联的属性。

我一直在研究 AMS 问题,似乎确实有一些关于应该如何处理(或者是否真的支持这种功能)的问题,但我很难确切地弄清楚当前状态是什么。

建议的解决方案之一是使用调用嵌套属性的方法覆盖属性,但这似乎被视为一种 hack,所以我想尽可能避免它。

无论如何,非常感谢您提供如何处理此或一般 API 建议的示例。

最佳答案

每次提交 1426:https://github.com/rails-api/active_model_serializers/pull/1426 - 和相关讨论,可以看到 jsonattributes 序列化的默认嵌套是一层。

如果你想默认深度嵌套,你可以在 active_model_serializer 初始化器中设置一个配置属性:

ActiveModelSerializers.config.default_includes = '**'

v0.10.6的详细引用:https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/adapters.md#include-option

关于ruby-on-rails - rails : Serializing deeply nested associations with active_model_serializers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32079897/

有关ruby-on-rails - rails : Serializing deeply nested associations with active_model_serializers的更多相关文章

随机推荐