views:

235

answers:

6

I'm making a site like StackOverflow in Rails but I'm not sure if it's necessary for the Votes on a question to be stored in a separate table in the database.

Is there any good reason to separate the data?

Or could I store the Votes as a single sum in a field of the Questions table?

+3  A: 

How would you know if a user voted on a question without keeping a votes table? Or like this website that holds you to X votes a day, how would you know how many votes a user made in the day? How would you keep track of how many up and down votes a user has done? I think good design practices pretty much scream for you to normalize the data and keep a votes table, with perhaps keeping a current +/- denormalized field in the question row for easy fetching.

Paolo Bergantino
+1 for mentioning normalization and optimizing for speed by denormalisation.
lexu
+1  A: 

Think about your data in multiple dimensions. There's more going on than the mere number of votes. There's:

  • Who cast the vote
  • When they cast the vote
  • The effect (think like a financial transaction) of the vote on any number of parties

Can you afford to discard this data? Will you ever need it? In Stackoverflow, it must be known whether I voted on something to determine if I can vote; what the vote was, so I can change it; the effect of the vote so it can be rolled back if I change it; etc.

Rex M
+5  A: 

Yes! Think about it from an object perspective. In model driven development (objects first) you would have a container (table) of questions, and a container of votes. Of course you could simply roll them up to an aggregate form. However by doing that you lose a lot of metric detail such as who cast the vote, when, etc. It really depends on if you need the detail or not. Space is cheap so not keeping the detail is usually not a good idea. It is hard to foresee what is needed in the future!

Andrew Siemer
Technically speaking, if you're thinking about objects, then a question could be said to "have a number of votes".
A: 

Votes would also need to be able to be applied to both questions and answers, although both questions and answers could be stored in the one table/class called Post or somthing similar since they are the same data with a different title.

railsninja
A: 

Like the last two answers say: keep a separate votes table.

But it would be advisable to create a view that will aggregate votes per user, per question etc. so that you don't need to do a manual query when you need that info.

jrh

Here Be Wolves
A: 

Yes, I would go so far as to say that it is vital to help reduce the likelyhood that one person could bias the result by repeatedly voting something up or down.

It actually has very little to do with OOP, and more to do with preventing exploits.

For performance reasons you could use a static vote count in the questions table that gets updated when the vote data for a question changes. I would not though only use a vote count by itself unless you really don't care about results being biased by particular people.