在 ActiveModelSerializer 中序列化与自定义字段的关系
Serialize relationships with custom fields in ActiveModelSerializer
我在使用 ActiveModelSerializers 正确序列化我的模型时遇到了一些问题。设置如下:
# controllers/posts_controller.rb
class PostsController < ApplicationController
def index
render json: @posts, each_serializer: PostSerializer, include: %w(comments), fields: post_fields
end
private
def post_fields
{ posts: [:name, :content, :author] }
end
end
# serializers/post_serializer.rb
class PostSerializer < ActiveModelSerializer
attributes :name, :content, :author
has_many :comments, serializer: CommentSerializer
end
当我向 posts#index
端点发出请求时,我期待 JSON 格式为 JSON-API 规范的响应,例如以下:
{
"data": {
"type": "post",
"id": 1,
"attributes": {
"name": "My first post",
"author": "Mr. Glass",
"content": "This is my first post ..."
},
"relationships": {
"comments": {
"data": {
"id": 1,
"type": "comments"
}
}
}
},
"included": {
"type": "comments",
"id": 1,
"attributes": {
"content": "First!"
}
}
}
然而,实际的响应是这样的:
{
"data": {
"type": "post",
"id": 1,
"attributes": {
"name": "My first post",
"author": "Mr. Glass",
"content": "This is my first post ..."
}
},
"included": {
"type": "comments",
"id": 1,
"attributes": {
"content": "First!"
}
}
}
而且整个关系块都不见了。有没有办法让实际响应中的关系块再次出现?
我想通了!
对于任何未来的读者 - 如果您想在 ActiveModelSerializers 中使用 relationships
块并使用 fields
选项,您需要执行以下操作:
# in your controller
render json: @posts, include: %(comments), fields: post_fields
def post_fields
# note the inclusion of "comments" in the array of fields to be
# serialized. That is the change you will need to make.
{ posts: [:name, :author, :content, :comments] }
end
希望对您有所帮助!
我在使用 ActiveModelSerializers 正确序列化我的模型时遇到了一些问题。设置如下:
# controllers/posts_controller.rb
class PostsController < ApplicationController
def index
render json: @posts, each_serializer: PostSerializer, include: %w(comments), fields: post_fields
end
private
def post_fields
{ posts: [:name, :content, :author] }
end
end
# serializers/post_serializer.rb
class PostSerializer < ActiveModelSerializer
attributes :name, :content, :author
has_many :comments, serializer: CommentSerializer
end
当我向 posts#index
端点发出请求时,我期待 JSON 格式为 JSON-API 规范的响应,例如以下:
{
"data": {
"type": "post",
"id": 1,
"attributes": {
"name": "My first post",
"author": "Mr. Glass",
"content": "This is my first post ..."
},
"relationships": {
"comments": {
"data": {
"id": 1,
"type": "comments"
}
}
}
},
"included": {
"type": "comments",
"id": 1,
"attributes": {
"content": "First!"
}
}
}
然而,实际的响应是这样的:
{
"data": {
"type": "post",
"id": 1,
"attributes": {
"name": "My first post",
"author": "Mr. Glass",
"content": "This is my first post ..."
}
},
"included": {
"type": "comments",
"id": 1,
"attributes": {
"content": "First!"
}
}
}
而且整个关系块都不见了。有没有办法让实际响应中的关系块再次出现?
我想通了!
对于任何未来的读者 - 如果您想在 ActiveModelSerializers 中使用 relationships
块并使用 fields
选项,您需要执行以下操作:
# in your controller
render json: @posts, include: %(comments), fields: post_fields
def post_fields
# note the inclusion of "comments" in the array of fields to be
# serialized. That is the change you will need to make.
{ posts: [:name, :author, :content, :comments] }
end
希望对您有所帮助!