【问题标题】:How to include child associations when serializing to json?序列化为 json 时如何包含子关联?
【发布时间】:2019-03-19 04:18:48
【问题描述】:

在使用 fast_jsonapi gem 之前,我是这样做的:

render json: school.to_json(include: [classroom: [:students]])

我的 SchoolSerializer 看起来像:

class SchoolSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :description, :classroom
end

如何让学生包含在 JSON 结果中?

此外,教室关联包括但它显示所有属性,有没有办法将教室属性映射到 ClassroomSerializer ?

class School < ApplicationRecord
  belongs_to :classroom
end

class Classroom < ApplicationRecord
  has_many :students
end

【问题讨论】:

  • 请用“学校”模型协会更新问题。
  • @OleksiiBaidan 我更新了模型详细信息。
  • 事实上,您可以将关联添加到您的序列化程序。 [文档] (github.com/Netflix/fast_jsonapi#serializer-definition)。当您在 School 序列化程序中设置 has_many: 教室时,rails 将尝试查找定义的 ClassroomSerializer 类并使用它进行渲染。因此,在您的序列化程序文件夹中创建 classtoom_serializer.rb。

标签: ruby-on-rails ruby fastjsonapi


【解决方案1】:
class SchoolSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :description

  belongs_to :classroom
end

# /serializers/classroom_serializer.rb
class ClassroomSerializer
  include FastJsonapi::ObjectSerializer
  attributes :.... #attributes you want to show
end

您还可以向学校模型添加其他关联,以访问学生。 像这样

has_many :students, through: :classroom

然后直接将其包含在 School 序列化程序中。

更新:另外请注意,您可以直接指向您需要的序列化程序类。 (如果您想使用与模型名称不同的类作为示例)。

class SchoolSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :description

  belongs_to :classroom, serializer: ClassroomSerializer
end

【讨论】:

  • 它的深度有限制吗?如果它喜欢:学校 -> 教室 -> 学生 -> ....
  • 我认为没有限制。只有常识。你必须明白:越深入,越慢。
  • 它看起来只显示关系的 id 和类型,而不是所有的道具。
  • 在序列化器中定义属性(你必须为你正在使用的所有模型创建它)
  • 我做到了,我为学校、教室和学生创建了序列化程序。
【解决方案2】:

render json: SchoolSerializer.new(school, include: "classrooms.students")

区别在于渲染序列化程序时使用“包含”。这告诉序列化器向返回的 JSON 对象添加一个“包含”键。

class SchoolSerializer
  include FastJsonapi::ObjectSerializer

  belongs_to :classroom
  has_many :students, through: :classroom

  attributes :school_name, :description
end
StudentSerializer
  include FastJsonapi::ObjectSerializer

  belongs_to :classroom
  belongs_to :school

  attributes :student_name
end

render json: SchoolSerializer.new(school).serialized_json

将返回一系列只有表格中顶级标识符的学生

data: {
  id: "123"
  type: "school"
  attributes: {
    school_name: "Best school for Girls",
    description: "Great school!"
    ...

  },
  relationships: {
    students: [ 
      {
        id: "1234",
        type: "student"
      },
      { 
        id: "5678",
        type: "student"
      }
    ]
  }
}

include: "classroom.students" 将返回完整序列化的学生记录,格式如下:

data: {
  id: "123"
  type: "school"
  attributes: {
    school_name: "Best school for Girls"
    ...

  },
  relationships: {
    classroom: {
      data: {
        id: "456",
        type: "classroom"
      }
    },
    students: [ 
      {
        data: {
          id: "1234",
          type: "student"
        }
      },
      { 
        data: {
          id: "5678",
          type: "student"
        }
      }
    ]
  },
  included: {
    students: {
      data { 
        id: "1234",
        type: "student",
        attributes: {
          student_name: "Ralph Wiggum",
          ...
        },
        relationships: {
          school: {
            id: "123",
            type: "school"
          },
          classroom: {
            id: "456",
            type: "classroom"
          }
        }
      }, 
      data: {
        id: "5678",
        type: "student",
        attributes: {
          student_name: "Lisa Simpson",
          ...
        },
        relationships: {
          school: {
            id: "123",
            type: "school"
          },
          classroom: {
            id: "456",
            type: "classroom"
          }
        }
      }
    },
    classroom: {
      // Effectively
      // ClassroomSerializer.new(school.classroom).serialized_json
    },
  }
}

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多