Ruby on rails 序列化为json时如何包含子关联?

Ruby on rails 序列化为json时如何包含子关联?,ruby-on-rails,ruby,fastjsonapi,Ruby On Rails,Ruby,Fastjsonapi,在使用fast_jsonapi gem之前,我做了以下工作: render json: school.to_json(include: [classroom: [:students]]) 我的SchoolSerializer看起来像: class SchoolSerializer include FastJsonapi::ObjectSerializer attributes :name, :description, :classroom end 如何将学生包括在JSON结果中 另外

在使用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
班级学校
您还可以向学校模型中添加其他关联,以访问学生。 像这样

has_many :students, through: :classroom
然后直接将其包含在学校序列化程序中

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

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

has_many :students, through: :classroom
然后直接将其包含在学校序列化程序中

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


render json:SchoolSerializer.new(学校,包括:“教室.学生”)

区别在于在呈现序列化程序时使用“include”。这告诉序列化程序向返回的JSON对象添加一个键“included”

类序列化程序
包含FastJsonapi::ObjectSerializer
属于:教室
有很多学生,通过教室
属性:学校名称,:描述
结束
StudentSerializer
包含FastJsonapi::ObjectSerializer
属于:教室
属于:学校
属性:学生姓名
结束

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"
      }
    ]
  }
}
包括:“教室.学生”
将以以下形式返回完整的序列化学生记录:

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


render json:SchoolSerializer.new(学校,包括:“教室.学生”)

区别在于在呈现序列化程序时使用“include”。这告诉序列化程序向返回的JSON对象添加一个键“included”

类序列化程序
包含FastJsonapi::ObjectSerializer
属于:教室
有很多学生,通过教室
属性:学校名称,:描述
结束
StudentSerializer
包含FastJsonapi::ObjectSerializer
属于:教室
属于:学校
属性:学生姓名
结束

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"
      }
    ]
  }
}
包括:“教室.学生”
将以以下形式返回完整的序列化学生记录:

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


请用“学校”模型关联更新问题。@OleksiiBaidan我用模型详细信息更新了问题。事实上,您可以将关联添加到序列化程序中。[文件]()。当您在学校序列化程序中设置has_many:classks时,rails将尝试查找已定义的ClassroomSerializer类,并使用它进行渲染。因此,请在serializers文件夹中创建classtoom_serializer.rb。请使用“学校”模型关联更新问题。@OleksiiBaidan我已使用模型详细信息进行了更新。事实上,您可以向序列化程序添加关联。[文件]()。当您在学校序列化程序中设置has_many:classks时,rails将尝试查找已定义的ClassroomSerializer类,并使用它进行渲染。所以,在serializers文件夹中创建classtoom_serializer.rb。它的深度有限制吗?如果它像:学校->教室->学生->我想没有限制。只有常识。你必须理解:你走得越深-你走得越慢。它看起来只显示关系的id和类型,而不是所有的道具。在序列化器中定义属性(你必须为你使用的所有模型创建属性)。我做到了,我为学校、教室和学生创建了序列化器。它能走多深有限制吗?如果它像:学校->教室->学生->我想没有限制。只有常识。你必须理解:你走得越深,你走得越慢。看起来它只显示关系的id和类型,而不是所有的道具。在序列化器中定义属性(你必须为你使用的所有模型创建属性)。我做到了,我为学校、教室和学生创建了序列化器。