首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActiveModelSerializer:如何给自定义属性添加关联?

ActiveModelSerializer:如何给自定义属性添加关联?
EN

Stack Overflow用户
提问于 2020-04-11 20:42:43
回答 2查看 100关注 0票数 0

我有一个带有自定义属性序列化程序,我需要包含一个到该自定义属性的关联,但无法弄清楚:

代码语言:javascript
复制
def dependent_integrations
  object.integrations.includes(:service_integrations).where(service_integrations: { master: false}).map do |integration|
    # this.integration.organization_integrations ===> I need to include organization_integrations into to integration object for serializer
  end
end

并获取代码的JSON:

代码语言:javascript
复制
"dependent_integrations": [
                {
                    "id": 2,
                    "name": "Confluence Cloud",
                    "key": "confluence-cloud",
                    "description": "blablaabla",
                    "vendor": "Atlassian",
                    "active": true,
                    "created_at": "2020-04-08T18:16:01.000Z",
                    "updated_at": "2020-04-08T18:16:03.000Z",
                    "custom": false
                },
            ]
        },

但我需要获得以下包含organization_integrations的JSON:

代码语言:javascript
复制
"dependent_integrations": [
                {
                    "id": 2,
                    "name": "Confluence Cloud",
                    "key": "confluence-cloud",
                    "description": "blablaabla",
                    "vendor": "Atlassian",
                    "active": true,
                    "created_at": "2020-04-08T18:16:01.000Z",
                    "updated_at": "2020-04-08T18:16:03.000Z",
                    "custom": false,
                    "organization_integrations": [
                        {
                           id: 1,
                           .......
                        },
                        {
                           id: 2,
                           .......
                        }
                    ]
                },
            .........
            ]
        },
EN

回答 2

Stack Overflow用户

发布于 2020-04-17 03:25:20

尝试在render方法中使用include选项。

代码语言:javascript
复制
def your_action
  # ...
  render(
    :json,
    include: 'parent_model.integrations.service_integrations',
    # ...
  )
end
票数 0
EN

Stack Overflow用户

发布于 2020-04-17 09:10:50

您想要的是在序列化程序中呈现一个关联。你需要做的是为你渲染的每个模型使用不同的序列化程序。请注意,您没有指定正在使用的rails版本,因此以下代码可能无法正常工作

代码语言:javascript
复制
# your serializer
def dependent_integrations
  ActiveModel::Serializer::CollectionSerializer.new(integrations, each_serializer: IntegrationSerializer).as_json
end

private

def integrations
  object.integrations.includes(:service_integrations).where(service_integrations: { master: false })
end

# IntegrationSerializer
class IntegrationSerializer < ActiveModel::Serializer
  attributes :id, :name, :key, :description, :vendor, :created_at, :updated_at, :custom, :organization_integrations

  def organization_integrations
    ActiveModel::Serializer::CollectionSerializer.new(object.organization_integrations, each_serializer: OrganizationIntegrationSerializer).as_json
  end
end

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

https://stackoverflow.com/questions/61156989

复制
相关文章

相似问题

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