user3154958
user3154958

Reputation: 5

Ruby on Rails Logic

I know the title might be ambiguous but I was unsure what heading it would come under. I am not sure if there is a "best practice" on this so here it goes...

I have a basic social network application I am building from scratch. Users can post a status and people can comment on that status, people should also be able to comment on a comment. I think I understand how it should work but wouldn't mind someone else giving me their point of view. So (association-wise) a status has_many comments and in turn a comment has just one status. Comments have_many subcomments and subcomments belong to just one comment. How do these nested comments work?

If a user posts a status and another user posts a comment; that's fine. However, if.... user 1 posts a status and user 2 comments on that status, user 3 then comments on user 2's comment (so user 1 has created a status, user 2 a comment and user 3 a subcomment) what happens when user 2 replies to user 3's subcomment? is it still classes as a subcomment (ie.. a comment on a comment) or would it have to be nested even further like so:

User 1 creates a status, user 2 creates a comment, user 3 creates a subcomment, user 2 creates a subsubcomment

That doesn't seem like the correct way to do it as it would eventually get too messy if users were replying to eachothers comments on the same status.

Update: Looking into associations another possibility would be that a status has_many comments and a comment has_many replies. But that doesn't solve my problem... what if a user replys to another users reply on a comment? a status has_many comments, a comment has_many replies.... but a reply also has_many replies?

Upvotes: 0

Views: 109

Answers (2)

Igor Drozdov
Igor Drozdov

Reputation: 15045

#262 Trees with Ancestry - RailsCasts - describes using gem for building nested messages.

Upvotes: 1

Ivan Denysov
Ivan Denysov

Reputation: 1502

I always use awesome_nested_set gem for such cases. You will only need one model for all comments. More about nested sets and their usage in general you can read on wiki page(It has really simple logic). For ready to use solutions check acts_as_commentable_with_threading gem's page.

Upvotes: 0

Related Questions