Rails: Best association model for Users ->posts -> comments model in a forum kinda of website?

前端 未结 1 1510
天命终不由人
天命终不由人 2021-02-10 00:10

I\'m creating a forum website, where each registered user can write many posts and
each post can have many comments.
Also each user can comment on any posts created by a

1条回答
  •  温柔的废话
    2021-02-10 00:37

    Since your post has many comments, so it is post.comments instead of post.comment

    Since comments is a list of comments, comments.user is not valid also.

    You will need the comment's id so that you could find the specific comment's user:

    post.comments.find(params[:id]).user
    

    of course, you could also get all users:

    post.comments.all.collect(&:user)
    

    0 讨论(0)
提交回复
热议问题