【问题标题】:ActiveModelSerializer: how to add association to custom attribute?ActiveModelSerializer:如何为自定义属性添加关联?
【发布时间】:2020-04-11 12:42:43
【问题描述】:

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

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:

"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
                },
            ]
        },

但我需要获取以下包含组织集成的 JSON:

"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,
                           .......
                        }
                    ]
                },
            .........
            ]
        },

【问题讨论】:

    标签: ruby-on-rails active-model-serializers


    【解决方案1】:

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

    def your_action
      # ...
      render(
        :json,
        include: 'parent_model.integrations.service_integrations',
        # ...
      )
    end
    

    【讨论】:

      【解决方案2】:

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

      # 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
      

      【讨论】:

        猜你喜欢
        • 2018-02-26
        • 2012-07-10
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 2011-11-28
        • 2023-03-17
        相关资源
        最近更新 更多