我确实发现了一些关于 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
这是正确的方法吗?