views:

54

answers:

1

I did find some questions on SO about Rails associations that are somewhat like my question, but for the life of me I can't seem to understand how to use belongs_to multiple models.

Here's the table structure I intend to have:

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"

And the associations:

User
 has_many :posts
 has_many :comments

Post
 belongs_to :user
 has_many :comments

Comment
 belongs_to :user
 belongs_to :post

Is this the correct approach?

+5  A: 

Yes that is the correct approach.

Maxem
Does this require a join table or anything... or would it work as expected with just what he has shown?
Andrew
These are one to many relationships (one user has many posts etc.), therefore no. The comments table should look like this: id user_id post_id and ofc content or whatever.
Maxem