Reputation: 4940
I'm using the thumbs_up gem to get votes(likes) on Posts.
I have a page for each user's statistics and one of the statistics I'm trying to find shows how many people have voted (liked) the current_user's Posts. Here is what I have so far, I just need to include something that shows only the count that was on the current_users posts.
@vote_count = Vote.where("voteable_type = ?", "Update").count
# This shows all of the votes on all of the updates instead of ONLY the vote count of the current_user's updates
The Votes table has these columns
voteable_id
voteable_type
voter_id
voter_type
...
...
I think I have to associate the voteable_id to the current_user's update_id
but I can't figure it out.
Vote Model
scope :for_voter, lambda { |*args| where(["voter_id = ? AND voter_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :for_voteable, lambda { |*args| where(["voteable_id = ? AND voteable_type = ?", args.first.id, args.first.class.base_class.name]) }
scope :recent, lambda { |*args| where(["created_at > ?", (args.first || 2.weeks.ago)]) }
scope :descending, lambda { order("created_at DESC") }
belongs_to :voteable, :polymorphic => true
belongs_to :voter, :polymorphic => true
attr_accessible :vote, :voter, :voteable if ActiveRecord::VERSION::MAJOR < 4
# Comment out the line below to allow multiple votes per user.
validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]
Edit
# user.rb
has_many :updates
# update.rb
belongs_to :user
Upvotes: 1
Views: 180
Reputation: 4232
Try with:
user.updates.joins(:votes).where(votes: { vote: true }).count
Upvotes: 1