DMH
DMH

Reputation: 2809

Display users who voted on post acts_as_votable

I am using the acts_as_votable gem and have implemented it so that a user can vote on a post model. What I want to do is when I display the posts I want to list what users voted for it. I currently display the score by:

<%= post.cached_votes_score %> 

In the posts/index.html.erb This works great. I just cant get my head around how to display the users who voted. Ideally I want to show user.image.

Here is the code so far:

model/concern/votes.rb

def up_vote
  @post = find_post
  @post.liked_by current_user
  flash[:notice] = "Thanks for voting #{current_user.name}"
  redirect_to @post
end

Iv added it so that user is the voter and the post is votable.

I also added cached migration:

class AddCachedVotesToPosts < ActiveRecord::Migration
 def self.up
 add_column :posts, :cached_votes_total, :integer, :default => 0
 add_column :posts, :cached_votes_score, :integer, :default => 0
 add_column :posts, :cached_votes_up, :integer, :default => 0
 add_column :posts, :cached_votes_down, :integer, :default => 0
 add_column :posts, :cached_weighted_score, :integer, :default => 0
 add_column :posts, :cached_weighted_total, :integer, :default => 0
 add_column :posts, :cached_weighted_average, :float, :default => 0.0
 add_index  :posts, :cached_votes_total
 add_index  :posts, :cached_votes_score
 add_index  :posts, :cached_votes_up
 add_index  :posts, :cached_votes_down
 add_index  :posts, :cached_weighted_score
 add_index  :posts, :cached_weighted_total
 add_index  :posts, :cached_weighted_average
end

def self.down
 remove_column :posts, :cached_votes_total
 remove_column :posts, :cached_votes_score
 remove_column :posts, :cached_votes_up
 remove_column :posts, :cached_votes_down
 remove_column :posts, :cached_weighted_score
 remove_column :posts, :cached_weighted_total
 remove_column :posts, :cached_weighted_average
end
end

Any help would be very much appreciated. Also do let me know if you want me to add any code above.

EDIT Here is link to the gem https://github.com/ryanto/acts_as_votable

Upvotes: 2

Views: 1337

Answers (2)

Icicle
Icicle

Reputation: 1174

You can get list of all voters id for the post using below code

 @post.votes_for_ids

Upvotes: 3

Nidhi
Nidhi

Reputation: 340

Can you try below code to get user name

@post.vote_by 

Upvotes: -1

Related Questions