首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在序列化程序中将表的字段添加到父表中

在序列化程序中将表的字段添加到父表中
EN

Stack Overflow用户
提问于 2018-06-01 20:39:48
回答 1查看 58关注 0票数 -1

我有三个表users,posts和comments。post的序列化程序是

代码语言:javascript
复制
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注释序列化程序是

代码语言:javascript
复制
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时,我需要评论的属性。

EN

回答 1

Stack Overflow用户

发布于 2018-06-02 03:58:33

只需在PostSerializer中的has_many关联中传递serializer选项

变化

has_many :comments

has_many :comments, serializer: CommentSerializer

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50643656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档