views:

151

answers:

5

I don't even know what questions I should ask. Well, I want to create a thumbs up for my comments, but not sure how or what's the best way. Do I just create a new field for thumbs up?

+1  A: 

It depends on what you want to do with this. Why not just put an int column on your comments table, storing the total number of thumbs up / down for the comment?

marcc
+4  A: 

Just storing an int for the number of times a comment has been voted up would be subject to abuse. You probably also want to associate each vote with the user who cast it, that way you can prevent people from repeatedly voting for the same comment.

For this to work, I think you'll need a separate table for votes. Each record in that table should have the comment id and the user id of the person who cast the vote.

Bill the Lizard
+4  A: 

If you need to keep track of who's voted on what, you should perhaps make a Votes table:

  • vote_id: Primary key.
  • user_id: The id of the user who made this vote. [Foreign key to Users table.]
  • comment_id: The id of the comment that was voted on. [Foreign key to Comments table.]
  • vote: The vote that was cast (perhaps +1 or -1 if you only have a trivial thumbs up/down system).
  • date: When the vote was cast.

A comment's score is now just the sum of all the vote columns which have that comment_id.

Note that unlike simply adding an integer score column to your Comments table, this has the advantage of telling you the level of controversy a comment is experiencing. Without knowing how many votes were cast, two comments with a net score of zero could either be experiencing a lot of controversy (people are equally split about the merit of the comment, so the total score hovers around 0), or none at all (nobody cares enough to cast a vote).

John Feminella
vote_id is actually unnecessary, unless a user can place more than one vote on the same comment.
Can Berk Güder
@Can: I presume it would be possible to do things like retract your vote once it's cast, and only the last one counts.
John Feminella
If you use MySQL (and possibly other DBs), it will create unique id column for you anyway if you don't create one.
Peter Štibraný
@John, Can: No, I think it is more a question of database design. While in current situation vote_id is (technically!) unneccessary, it seems a good idea to me to use a separate identity column for the pk and declare a key on user_id and comment_id (to make them "unique together"). It is simply more flexible that way and costs you (almost) nothing.
shylent
@Peter: Really? That, certainly, is news to me. Does mysql *really* do that, or is it just a GUI of some sort you are using (mysql query browser?) making it for you?
shylent
This was an amazing answer! Thanks for explaining everything I need. :D
Doug
@shylent: I was too quick, and not very specific. This is not MySQL feature, but InnoDb engine feature: http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html
Peter Štibraný
A: 

Creating a separate table, as Bill and John have suggested, would probably be the best approach. But you might still want to add a votes column to the comments table for performance reasons. This way, you won't need to access the votes table when you only want to display the vote count for a comment. I believe this is how votes work on SO.

Can Berk Güder
A: 

Create 2 fields in your comment table, vote_up and vote_down, and increase their counters accordingly upon user's vote, this way you can display comment score as sum of these values or as a percentage, in the later case you could add third field vote_score which stores percentage score if you ever wanted to be able to sort by score.

Then create votes table to prevent users voting twice the same comment, ever or in given time span, if so just set cron job to run once a day and delete entries older than time()-( 86400 * DAYS_TO_KEEP_VOTE )

  • comment_id
  • user_id
  • vote_time

Good luck.

Niteriter