我有三个表users,posts和comments。post的序列化程序是
class PostSerializer < ActiveModel::Serializer
attributes :id, :content, :like, :created_at, :updated_at
attributes :user
has_many :comments
def user
object.user
end
end
注释具有以下attributes.The注释序列化程序是
class CommentSerializer < ActiveModel::Serializer
attributes :id, :comment, :created_at, :updated_at
attributes :user
attributes :post
belongs_to :user
belongs_to :post
def user
object.user
end
def post
object.post
end
end
当我打印文章的JSON时,我需要评论的属性。
发布于 2018-06-02 03:58:33
只需在PostSerializer
中的has_many
关联中传递serializer
选项
变化
has_many :comments
至
has_many :comments, serializer: CommentSerializer
希望这能有所帮助
https://stackoverflow.com/questions/50643656
复制相似问题