Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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 Twitter ActiveRecord声誉:将主要声誉计算为非主要声誉_Ruby On Rails - Fatal编程技术网

Ruby on rails Twitter ActiveRecord声誉:将主要声誉计算为非主要声誉

Ruby on rails Twitter ActiveRecord声誉:将主要声誉计算为非主要声誉,ruby-on-rails,Ruby On Rails,我有两个模型:选民模型和民意测验模型 投票人可以通过两种方式进行投票:1)向上/向下投票2)权力投票 我有向上/向下投票,但我想知道一个用户总共累积了多少次权力投票,因为他们只能拥有一定数量的权力投票 poll.rb class Poll < ActiveRecord::Base belongs_to :voter has_reputation :votes, source: :voter has_reputation :powervote

我有两个模型:选民模型和民意测验模型

投票人可以通过两种方式进行投票:1)向上/向下投票2)权力投票

我有向上/向下投票,但我想知道一个用户总共累积了多少次权力投票,因为他们只能拥有一定数量的权力投票

poll.rb

class Poll < ActiveRecord::Base
  belongs_to :voter

  has_reputation :votes,
                 source: :voter

  has_reputation :powervotes,
                 source: :voter,
                 source_of: [{reputation: :voter_powervotes, of: :voter}]

  has_reputation :score,
                 source: [
                   { reputation: :votes },
                   { reputation: :powervotes, weight: 2 }]

  def upvote(voter)
    self.add_or_update_evaluation(:votes, 1, voter)
  end

  def downvote(voter)
    self.add_or_update_evaluation(:votes, -1, voter)
  end

  def powervote(voter)
    self.add_or_update_evaluation(:powervotes, 1, voter)
  end

  def score
    self.reputation_for(:score).to_i
  end
end
class Voter < ActiveRecord::Base
  has_many :polls

  has_reputation :voter_powervotes,
                 source: [reputation: :powervotes, of: :polls]

  def powervotes
    self.reputation_for(:powervotes).to_i
  end
end
class Poll
voter.rb

class Poll < ActiveRecord::Base
  belongs_to :voter

  has_reputation :votes,
                 source: :voter

  has_reputation :powervotes,
                 source: :voter,
                 source_of: [{reputation: :voter_powervotes, of: :voter}]

  has_reputation :score,
                 source: [
                   { reputation: :votes },
                   { reputation: :powervotes, weight: 2 }]

  def upvote(voter)
    self.add_or_update_evaluation(:votes, 1, voter)
  end

  def downvote(voter)
    self.add_or_update_evaluation(:votes, -1, voter)
  end

  def powervote(voter)
    self.add_or_update_evaluation(:powervotes, 1, voter)
  end

  def score
    self.reputation_for(:score).to_i
  end
end
class Voter < ActiveRecord::Base
  has_many :polls

  has_reputation :voter_powervotes,
                 source: [reputation: :powervotes, of: :polls]

  def powervotes
    self.reputation_for(:powervotes).to_i
  end
end
类投票者

当我以用户身份对投票进行powervote()时,然后调用Voter.powervotes,它返回0。有什么好处?我觉得我独自走到这一步是个奇迹,但现在我迷失了方向。也许这是不可能的,我只需要手动查询数据库?

这里有一个简单的解决方案,是在gem创建者的帮助下找到的。当您只需计算power\u投票的评估人时,无需在选民模型中创建:voter\u powervoces信誉。看到这件事有多简单,我觉得自己像个傻瓜。希望这对其他人有帮助

投票者.rb

def powervotes
  Poll.evaluated_by(:power_votes, self).count.to_i
end

GitHub问题参考:

如果我不够具体,我想知道为什么即使为powervote创建了一行,我也无法知道用户创建了多少powervote。再一次,也许我只需要做一个手动查询,通过过滤目标、来源和输入rs_评估来收集所有评估。好的,我想我已经找到了问题的一部分,我想这都指向STI。当我以用户身份在投票中记录powervote时,使用此设置,会在我的rs_声誉表中创建3个声誉。PowerVoces投票人\u PowerVoces评分一切似乎都很好,除非你再深入一点。您可以看到投票者的目标id似乎拉错了id。在本例中,它拉4-应该是1。我认为这是因为Poll ID 1有一个名为voter_ID的列,它的值是4。