I am currently developping an comment system using ruby on rails and React. Everything was working well, my User can post comments, and products can receive comments. But now I have a problem. Here is my three models:
The Product
class Product< ApplicationRecord
has_many :comments
end
The User
class User < ApplicationRecord
has_many :comments
end
and the Comment
class Comment < ApplicationRecord
belongs_to :Product
belongs_to :user
end
Now I would like to display render the Product on a product page, using JSONAPI::Serializer.
So I call the ProductSerializer:
def show
render json: ProductSerializer.new(@product, { include: [:comments] })
end
and here is the ProductSerializer:
class ProductSerializer
include JSONAPI::Serializer
attributes :id, :created_at, :description
has_many :comments
end
(I followed the official documentation on github here : https://github.com/jsonapi-serializer/jsonapi-serializer)
so now the problem is: why the json response does not include the comments? I have also tried :
class ProductSerializer
include JSONAPI::Serializer
attributes :id, :created_at, :description
has_many :comments, serializer: CommentSerializer
end
But it still does not work... And if I include comments within the attributes of the Serializer, like that:
class ProductSerializer
include JSONAPI::Serializer
attributes :id, :created_at, :description, :comments
end
I do receive the list of comment, but instead of an User object, I get an User_id and that is not what I want.
I am using rails 7.0.6 and jsonapi-serializer 2.2.0