-1

I have three tables users,posts and comments. The serializer of post is

class PostSerializer < ActiveModel::Serializer
    attributes :id, :content, :like, :created_at, :updated_at
attributes :user
has_many :comments
def user
    object.user
end
end

Comments have the following attributes.The comment serializer is

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

I need the attributes of comments when I print the JSON of posts.

indrapranesh
  • 41
  • 2
  • 6

1 Answers1

0

Simply pass the serializer option in your has_many association in PostSerializer

Change

has_many :comments

To

has_many :comments, serializer: CommentSerializer

Hope this helps

Nimish Gupta
  • 3,095
  • 1
  • 12
  • 20