【发布时间】:2020-09-08 16:38:19
【问题描述】:
这是我的 cmets 的数据结构。我认为我有错误的数据结构来递归地渲染 cmets。请指教。我可以获得单个 cmets 和一个回复,但不能获得未知数量的回复。
"comments": [
{
"id": 1,
"content": "This is so great!",
"recipe_id": 1,
"commentable_id": 1,
"commentable_type": "Recipe",
"user_id": 1,
"created_at": "2020-09-08T00:16:55.296Z",
"updated_at": "2020-09-08T00:16:55.296Z"
},
{
"id": 2,
"content": "This is another one",
"recipe_id": 1,
"commentable_id": 1,
"commentable_type": "Comment",
"user_id": 1,
"created_at": "2020-09-08T00:16:55.323Z",
"updated_at": "2020-09-08T00:16:55.323Z"
},
{
"id": 3,
"content": "This is another one",
"recipe_id": 1,
"commentable_id": 2,
"commentable_type": "Comment",
"user_id": 1,
"created_at": "2020-09-08T00:16:55.332Z",
"updated_at": "2020-09-08T00:16:55.332Z"
},
【问题讨论】:
-
ID 为 2 和 ID 为 3 的对象是 ID 为 1 的对象的子对象吗?也就是说,对于带有
commentable_type == 'Comment'的对象,commentable_id指的是评论 ID? -
@AKX 对象 ID 2 是 ID 1 的子对象,ID 3 是 ID2 的子对象。 Commentable_id 是指被评论的评论id。希望这能澄清一点