I have an app that’s a simple clone of reddit. Using Devise you can sign up and submit links and vote on them. I started out trying vote_fu_rails_3 but was having a db issue and some other troubles so I went with my own voting solution which just records the link_id, user_id and has timestamps.
I’m trying to implement a way for the votes on your links to count towards a total ‘karma’ score, ala reddit. Your karma would be your positive votes less your negative votes in total. I’m thinking I need to write a method in the User model (perhaps link model?)
Right now there is no field in the users table for ‘karma’ or ‘link_score’ or anything like that. Maybe adding a simple integer column to the Link table and adding or subtracting to it when it’s voted on would allow me to do this?
Right now to display the number of votes i’m using link.votes.count which may not be correct (maybe it shows total votes and not total as Up – Down).
I’m going to use the features of has_many
:votes, :through => :linksand the sum method.For additional information check:
so here’s the solution:
User Table
Links Table
Vote Table
User Model
Link Model
Vote Model
The trick is that you set the score to a positive or negative value let’s say “+1” for a positive vote and “-1” for a negative vote. NOTE: Every vote is a record. The sum will be the total score.
How to use:
There are other features you can use like a vote of a “trusted” user could score +5 or -5 etc. etc.
Enjoy!