Domanda

I'm using this gem for comments: https://github.com/lml/commontator

Which is setup to easily plug into this gem to vote on the comments: https://github.com/ryanto/acts_as_votable

Everything seems to be working fine. But when trying to calculate a users total votes (the total votes received on all comments by the user) (karma)

<%= @user.votes.count %>

I get this error

undefined method `votes' for #<User:0x0000010dbf23a0>

So I tried this:

<%= @user.comments.map{|c| c.votes.count}.inject(:+) %>

Which resulted in another error:

SQLite3::SQLException: no such column: commontator_comments.commontator_id: SELECT "commontator_comments".* FROM "commontator_comments"  WHERE "commontator_comments"."commontator_id" = ? AND "commontator_comments"."commontator_type" = ?

How do I render the total number of votes received on all comments by a particular user?

È stato utile?

Soluzione

Assuming you have this setup

class User < ActiveRecord::Base
  acts_as_voter
  acts_as_commentator
  has_many :comments
end

class Comment < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
end

And the following after installing commentator and acts_as_votable gems

rails generate acts_as_votable:migration
rake commontator:install
rake db:migrate

You should be able to get the number of votes like this

@user.comments.collect{|c| c.votes.size}.inject(:+)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top