Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:53:36+00:00 2026-05-20T09:53:36+00:00

I have an app that’s a simple clone of reddit. Using Devise you can

  • 0

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).

Github Link

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:53:37+00:00Added an answer on May 20, 2026 at 9:53 am

    I’m going to use the features of has_many :votes, :through => :links and the sum method.

    For additional information check:

    • Ruby on Rails Guide on Associations
    • Ruby on Rails Guide on Calculations

    so here’s the solution:

    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
    

    Links 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
    

    Vote Model

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

    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:

    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 features you can use like a vote of a “trusted” user could score +5 or -5 etc. etc.

    Enjoy!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that lets the user interact with several forms. I can
I just need to play a simple sound, I have app that send messages,
I have an app that uses fragments.I'm using backward compability package with Android 2.2.
I have a app that I'm deploying to a development server using Capistrano. I'd
I have an App that will send authenticated emails using System.Net.Mail and System.Net.NetworkCredential my
Have an app that can use tts to read text messages. It can also
I have an app that needs to import a .sql file. I can import
I have an app that is been tracked using git and the repository is
I have an app that is using google analytics to track usage of the
have an app that finds your GPS location successfully, but I need to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.