代码之家  ›  专栏  ›  技术社区  ›  Zabba fl00r

轨道属于许多型号[关闭]

  •  49
  • Zabba fl00r  · 技术社区  · 14 年前

    我确实发现了一些关于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
    

    这是正确的方法吗?

    2 回复  |  直到 7 年前
        1
  •  52
  •   Maxem    14 年前

        2
  •  10
  •   thutt    7 年前