0

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

I'm using rails 4 btw, which is compatible with both gems.

In my User model:

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

In my Comment model:

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

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" = ?

I've tried:

@user.find_voted_items.count

and

@user.get_voted(Comment).count ? 

and

@user.comments.collect{|c| c.votes.size}.inject(:+)

Nothing seems to work. I'm guessing it has to do with the way the commontator gem is handling the proxy associations relationship. How do I render the total number of votes received on all comments by a particular user? Any help is very much appreciated!

Edit: I did run all the migrations.

Katie H
  • 2,283
  • 5
  • 30
  • 51
  • did you run the migrations? – Rubyman Feb 14 '14 at 14:05
  • Yup! I should have mentioned that! – Katie H Feb 14 '14 at 14:08
  • What do you get with '@user.find_voted_items.count'? – BroiSatse Feb 14 '14 at 14:11
  • That does not give me an error, but gives me the number of items that particular user has voted on, not the number of votes he has received on all of his comments. – Katie H Feb 14 '14 at 14:17
  • 1
    In the commontator gem page, there's a section that describes how to integrate with acts_as_votable. Did you check that part? https://github.com/lml/commontator#voting. It seems that you have to add that setting, and then restart your app. – Hesham Feb 14 '14 at 14:40
  • Yup, I set config.can_vote_on_comments = true in the initializer and restarted the app. – Katie H Feb 14 '14 at 14:42
  • 1
    @KatieHeidmann I wonder if `has_many :comments` in the user model is causing all of this. When you include acts_as_commentator in your model, it already adds the relationship for you. – Hesham Feb 14 '14 at 16:20
  • Hmm, what do you think I should have in my user model? Should I even have a comment model? Commentator already provides one. – Katie H Feb 14 '14 at 16:33
  • Yes, it does provide it for you. You don't need a comment model. Also, quick question: did you run `rake commontator:install` when you added the gem? – Hesham Feb 14 '14 at 16:34
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47518/discussion-between-h-man-and-katie-heidmann) – Hesham Feb 14 '14 at 16:36

1 Answers1

1

Shouldn't you be counting votes on the comments? The User model acts_as_voter so, according to the docs on the gem, you can retrieve a list of the items a user has voted on with find_voted_items, but the Comment model is the one where you can count votes since that's what the user is voting on.

Edit, given the comments. At it's simplest, you probably need something similar to this:

sum = 0
@user.comments.each do |comment|
    sum += comment.votes.count
end

though you can probably make that a bit more eloquent with inject or even with Activerecord#sum on the votes field with a carefully constructed "where clause".

slapthelownote
  • 4,249
  • 1
  • 23
  • 27
  • Yup, that's what i'm trying to do. I'm trying to render the sum score of all the comments that particular user has made. Sort of like their karma. What method should I use? – Katie H Feb 14 '14 at 14:18
  • 1
    Well, I've only read the docs for the gem, but I would think you would want get the collection of comments a user has made and then iterate over them getting the score for each comment. – slapthelownote Feb 14 '14 at 14:42
  • That gives me: 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" = ? – Katie H Feb 14 '14 at 16:12
  • 1
    Okay - well, a little tricky to debug without seeing more of your code - what's in your schema.rb file, or if you have the project on github point us there.... – slapthelownote Feb 14 '14 at 19:21