Rails 3 the best way to realize the idea of ​​karma?

I have an application that is a simple reddit clone. Using Devise, you can subscribe and send links and vote for them. I started trying vote_fu_rails_3, but had a problem with db and some other problems, so I went with my own voting solution that just writes link_id, user_id and has timestamps.

I am trying to implement a voting method on your links in order to calculate the total karma score, ala reddit. Your karma will be your positive vote less than your negative voices. I think I need to write a method in the User model (perhaps a reference model?)

Right now in the user table there is no field for 'karma' or 'link_score' or something like that. Maybe adding a simple integer column to the link table and adding or subtracting it on the vote would allow me to do this?

Right now, to display the number of votes, I am using link.votes.count, which may be incorrect (maybe this shows the total number of votes, and not the total as Up-Down).

github link

+4
source share
2 answers

I am going to use the has_many :votes, :through => :links and sum functions.

For more information check:

so the solution is:

User table

 class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.timestamps end end def self.down drop_table :users end end 

Reference table

 class CreateLinks < ActiveRecord::Migration def self.up create_table :links do |t| t.integer :user_id t.string :url t.timestamps end end def self.down drop_table :links end end 

Vote table

 class CreateVotes < ActiveRecord::Migration def self.up create_table :votes do |t| t.integer :user_id t.integer :link_id t.integer :score t.timestamps end end def self.down drop_table :votes end end 

User model

 class User < ActiveRecord::Base has_many :links has_many :votes, :through => :links def karma self.votes.sum(:score) end def positive_votes self.votes.sum(:score, :conditions => 'score > 0') end def negative_votes self.votes.sum(:score, :conditions => 'score < 0') end end 

Link Model

 class Link < ActiveRecord::Base belongs_to :user has_many :votes end 

Voting model

 class Vote < ActiveRecord::Base belongs_to :user belongs_to :link end 

The trick is that you set the score to positive or negative, let them say β€œ+1” for a positive vote and β€œ-1” for a negative vote. NOTE. . Each vote is a record. The amount will be a total estimate.

How to use:

 User.first.karma # gives you total karma User.first.positive_votes # gives you total positive votes User.first.negative_votes # gives you total negative votes 

There are other functions that can be used, for example, the voice of a "trusted" user can dial +5 or -5, etc. etc.

Enjoy it!

+2
source

If you want it to be fast, why not add a Karma to User model and update it when someone votes up / down? Otherwise, you will have to constantly calculate it every time it is displayed. It can become expensive if you get a lot of users with a lot of karma, which I believe is your goal.

+2
source

Source: https://habr.com/ru/post/1342525/


All Articles