0

在使用 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
4

2 回答 2

2
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
于 2018-10-14T17:53:03.327 回答
0

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

于 2019-12-11T03:21:25.867 回答