Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 查找用户在其帖子上的总投票数_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 查找用户在其帖子上的总投票数

Ruby on rails 查找用户在其帖子上的总投票数,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我用gem在帖子上获得投票(喜欢) 我为每个用户的统计信息提供了一个页面,我试图找到的一个统计信息显示有多少人投票(喜欢)了当前用户的帖子。这是我到目前为止所做的,我只需要包括一些只显示当前_用户帖子数量的内容 @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

我用gem在帖子上获得投票(喜欢)

我为每个用户的统计信息提供了一个页面,我试图找到的一个统计信息显示有多少人投票(喜欢)了当前用户的帖子。这是我到目前为止所做的,我只需要包括一些只显示当前_用户帖子数量的内容

@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
投票表包含以下列

voteable_id
voteable_type
voter_id
voter_type
...
...
我想我必须将voteable\u id与当前用户的
update\u id
关联起来,但我想不出来

投票模式

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]
尝试:

user.updates.joins(:votes).where(votes: { vote: true }).count

用户和投票设置之间的关系如何?另外,您如何知道投票是什么样的呢?在视图中,您可以通过
@update.voates.count
来判断,因此在
投票的数据库中,有一个布尔列
:vote
。这个gem的设置方式是在模型中,如果一个模型是voteable,你会把
acts\u作为voteable
,对于用户模型,你会把
acts\u作为voteable
。但是我假设一个
用户有很多:投票
投票属于:用户
更新有很多:投票
检查我的问题的更新是否有
投票
模型没有
用户.投票。其中('voteable\u type=?and Vote=?','update',true)。计算一下你需要什么?这将显示更新投票数。这将显示当前用户更新投票的次数。我需要当前\u用户更新的投票数。
错误:列更新.voteable\u类型不存在
。奇怪。看来我的评论被删除了?还有5个小时我才能赏金。
user.updates.joins(:votes).where(votes: { vote: true }).count