I need to set up a simple voting system for my application., My application consists of articles posted as well as comments. I would like to add voting abilities to both articles and comments and at the same time be able to sort comments based upon highest voted etc.
I have the following restrictions i.e since the application needs users to log in - only logged in users can vote, secondly a user can vote on an item only once. Users can upvote or downvote or cancel a vote they've made.
What would be a decent table design for this, p开发者_运维百科lus I need the solution to be scaleable. Thanks for the advice
I think I would go with a join-table between the users
and articles
tables :
users_articles
- article_id
- user_id
- score
- date
With the following notes :
article_id
is a foreign key to the article that gets up/down-voteduser_id
is a foreign key to the user that votedscore
is +1 or -1 depending on the vote- the primary key is on the two
article_id, user_id
columns. - a user voting on an article means inserting one line in this table ; canceling the vote means deleting that line (or setting a 0 score if you want to keep track of the fact the user has voted)
That's for votes on articles.
And I would do another users_comments
table for the votes on comments.
精彩评论