我確實發現了一些關於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"
而且協會:
User
has_many :posts
has_many :comments
Post
belongs_to :user
has_many :comments
Comment
belongs_to :user
belongs_to :post
這是正確的做法?
如何繼續執行備註保存? – 2017-08-08 12:21:06
omg,自從我學習rails以來已經7年了(謝謝你!)。要回答你的問題@MosesNdeda,你將實例化一個評論,分配用戶和發佈對象,然後在Comment對象上調用'save'。 – Zabba 2017-08-11 05:47:49