查找其他用户在帖子上收到的用户总票数 [英] Finding user total votes recieved on posts by other users

查看:137
本文介绍了查找其他用户在帖子上收到的用户总票数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此宝石进行评论: https://github.com/lml/commontator

通过设置,您可以轻松插入此宝石,对评论进行投票: https://github.com/ryanto/acts_as_votable



我使用的是rails 4 btw,它与两个gem兼容。 p>

在我的用户模型中:

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

在我的评论模型中:

  class评论< ActiveRecord :: Base 
acts_as_votable
belongs_to:user
end

一切似乎都工作正常。但是,当试图计算用户总票数(用户在所有评论中收到的总票数)时,(业力)

 < %= @ user.votes.count%> 

我得到这个错误

 未定义的方法`votes'for#< User:0x0000010dbf23a0> 

所以我试过了:

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

导致另一个错误:

  SQLite3 :: SQLException:no such column:commontator_comments.commontator_id:SELECTcommontator_comments。* FROMcommontator_commentsWHEREcommontator_comments。commontator_id=?和commontator_comments。commontator_type=? 

我试过了:

  @ user.find_voted_items.count 

  @ user.get_voted(评论).count? 

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

没有任何东西可以工作。我猜测它与commontator gem处理代理关联关系的方式有关。如何呈现特定用户在所有评论中收到的总票数?任何帮助非常感谢!

编辑:我确实运行了所有迁移。

解决方案

你不应该在评论上对选票进行点票吗?用户模型 acts_as_voter 因此,根据gem上的文档,您可以通过 find_voted_items 获取用户投票的项目列表。 code>,但评论模型是可以计数的那个模型,因为这是用户投票的内容。



编辑,给出评论。最简单的方法是,您可能需要类似以下内容:

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

尽管您可以用注入或甚至在投票字段上使用精心构建的where子句的Activerecord#sum来表达更多的话。


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.

解决方案

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".

这篇关于查找其他用户在帖子上收到的用户总票数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆