且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用active_model_serializers为嵌套对象选择序列化程序

更新时间:2023-09-28 23:32:04

我找到了解决方案.它不能自动执行,但是您可以使用以下命令对嵌套对象强制执行序列化程序:

I found a solution. It cannot be performed automatically but you can force a serializer on a nested object by using:

render json: {user_type: "student", student: StudentSerializer.new(stu)}

一个有趣的注释是,提起请求已提交,并且对于active_model_serializers https://github.com /rails-api/active_model_serializers/pull/300 会自动执行此任务.

An interesting note is that a pull request was submitted and remains in limbo for active_model_serializers https://github.com/rails-api/active_model_serializers/pull/300 which would preform this task automatically.