Zab*_*bba 53 activerecord ruby-on-rails belongs-to
我确实发现了一些关于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"
Run Code Online (Sandbox Code Playgroud)
而协会:
User
has_many :posts
has_many :comments
Post
belongs_to :user
has_many :comments
Comment
belongs_to :user
belongs_to :post
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
Max*_*xem 56
是的,这是正确的方法.
| 归档时间: |
|
| 查看次数: |
22314 次 |
| 最近记录: |