Question

I'm using the Active Record Reputation System gem and allow users to vote up or down on particular listings. I'm currently able to check whether a user has voted on a listing by:

if current_user && listing.has_evaluation?(:votes, current_user)

This tells me whether use user has voted or not, but I need similar logic to tell me whether they voted up or down for that particular listing. Any thoughts? Relevant code below:

Within listing model:

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

Within listing controller

  def vote
    value = params[:type] == "up" ? 1 : -1
    listing = Listing.find(params[:id])
    listing.add_or_update_evaluation(:votes, value, current_user)
    redirect_to :back, notice: "Thank you for voting"
  end
Was it helpful?

Solution

The goal in determining the value was so I could show whether the current user had liked a particular page. The @thing.has_evaluation?(:reputation_name, :source) was not working as it showed the page as "liked" even if a user liked, then disliked.

If anyone runs into this, you can delete an evaluation as the source level by calling the method delete_evaluation, then then a user's previous like/dislike/vote, etc is removed from the system and it's like they never voted.

Primarily helpful if you have a binary voting process

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top