ActiveRecord models that act as voteable can be queried for the positive votes, negative votes, and a total vote count by using the votes_for, votes_against, and votes_count methods respectively. Here is an example:

Love it? Share it!

This entry was posted on Saturday, June 24th, 2006 at 10:48 amand is filed under . You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

16 Responses to “Acts As Voteable Rails Plugin”

hey, lots of good stuff up here. sadly, I think the acts_as_voteable is broken. firstly the :submitted above should probably be :created_at as there is no submitted attribute in votes.

also the voted_for and voted_against methods use TRUE and FALSE which errors in mySQL since mySQL uses int(1) for boolean types. is there a reason for this? i hate to see good plugins fall out of service.

@Lawrence – Thanks for bringing this to my attention. On MySQL 4.1.15, the vote column is a tinyint(1) and I get no error casting votes. I guess I could just use 1/0 instead of TRUE/FALSE to cast a vote. Let me read up on booleans on MySQL…

Thanks for this plugin. There is one thing though, I would like to go for the route of allowing a user to vote from 1 through 5, instead of a basic vote for 1 point for e.g (that’s what your plugin currently acts as).

How would I implement such a functionality? Could it be done with some slight modification to your plugin?

I had a 1 N database problem with using acts_as_voteable when applied to a list of comments. I solved this with some updated code. If you are including in the votes table correctly, this will yield 0 extra database hits and hopefully squeeze out some extra performance.

Is a reputation system planned (i.e. each vote can have a weight based on the user that expressed it)?

If not, any idea on how it should be implemented?

Keeping the reputation count in User can be expensive when retrieving the vote count for a voteable (each vote would have to be multiplied by the reputation every time), but storing the reputation in the vote it’s not right because it varies with time.

[…] is a voting mixin that allows you to extend your models to vote on one another. Largely based on Cosmin Radoi’s acts_as_voteable plugin, VoteFu adds named_scope support, a set of generators to make using the plugin easier, a […]

I’m writing to ask you to rename this plugin. Its name is too generic; voting can comprise many other kinds of activity besides user ratings. (Traditionally, voting refers to a group choosing from a set of alternatives, a.k.a. candidates. There’s a rich body of academic literature known as “social choice theory.”) Perhaps acts_as_rateable would be a more appropriate name? Then the name acts_as_voteable could be reserved for a plugin that handles other kinds of voting.
The plugin for voting that I hope to find (or develop myself) would be similar to acts_as_list, in that it would facilitate allowing the voter to sort a collection of alternatives into his/her desired order of preference. It would differ from acts_as_list–assuming my understanding of acts_as_list is correct; I’m new to Rails–in that a voter’s order of preference may have more than one alternative per position. (Known as a “nonlinear” or “nonstrict” or “weak” ordering.) That would allow the voter to express indifference between those alternatives. There are several reasons to allow expression of indifference: (1) A voter may sincerely be indifferent between some alternatives. (2) The list of alternatives when initially presented to the voter should not necessarily be ordered since a pre-ordered ballot could affect the votes, so before the voter sorts the alternatives they would initially all appear at the same position. (3) In some elections there could be so many alternatives that it would be too tedious for voters to order them all; it would save time if the voter may leave some alternatives “unranked,” which would be treated as if the voter had ranked them together at the bottommost position. (4) It may be desirable to allow voters to strategically vote “insincere” indifference between some alternatives. (If you’re curious why that can be desirable, you can google the “Minimal Defense” criterion or the “Strong Defensive Strategy Criterion.”)
The reason I’m looking for such a plugin is because I want to construct an online voting server that implements the Maximize Affirmed Majorities (MAM) voting method and (if I have time) the Voting for a Published Ranking (VPR) voting method. If you want more details, feel free to ask me.
Thanks for considering this, and best wishes,
Steve