Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Tony Vidmer's Profile

GitHub User: awvidmer

Site: awvcommunications.com

Comments by Tony Vidmer

Avatar

As much as I love Ryan and all the Railscasts, this one is a bit confusing, for this reason:

When Ryan talks about "reputation_value_for" (now "reputation_for") as giving him the "total number of votes," it is in fact giving him the SUM OF THE VOTE VALUES. So, if one is looking for the actual total number of votes, regardless of vote value, the returned value is incorrect.

In fact, as far as I can tell, there is no generic way to get the total number of votes on a model without assigning a constant "1" to a vote tally value and using reputation_for(:vote_tally). So, in order to end up with:

78% of 281 voters like this Haiku. You like it, too.

...there is a need to add a THIRD reputation to get the count of ReputationSystem::Evaluations on the Haiku model, since we need to use:

has_reputation :votes, source: :user, aggregated_by: :sum
has_reputation :avg_vote, source: :user, aggregated_by: :average

...in order to get the user's vote (assigned as 1 = like or 0 = unlike) as well as the average vote. So we add:

has_reputation :vote_tally, source: :user, aggregated_by: :sum

Then, in the vote action in the controller:

v_value = params[:type] == "up" ? 1 : 0
@haiku.add_or_update_evaluation(:votes, v_value, current_user)
@haiku.add_or_update_evaluation(:avg_vote, v_value, current_user)
@haiku.add_or_update_evaluation(:vote_tally, "1", current_user)

If there's an easier way to get the total number of votes on a model, I'd love to hear about it.