Closed. This question is opinion-based。它当前不接受答案。












想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。

2年前关闭。



Improve this question




我确实在SO上找到了一些有关Rails关联的问题,但与我的一生一样,我似乎无法理解如何使用belongs_to多个模型。

这是我打算拥有的表结构:
User
 id

Post
 id
 user_id #foreign key; a post belongs to a User aka "Who created this post"

Comment
 id
 user_id #foreign key; a comment belongs to a User aka "Who made this comment"
 post_id #foreign key; a comment belongs to a Post aka "What post this comment is for"

和关联:
User
 has_many :posts
 has_many :comments

Post
 belongs_to :user
 has_many :comments

Comment
 belongs_to :user
 belongs_to :post

这是正确的方法吗?

最佳答案

是的,那是正确的方法。

10-08 05:01