我正在玩 Active Record Reputation gem https://github.com/twitter/activerecord-reputation-system/wiki/Scope-Reputations。在我的应用程序中,我创建了一个信誉系统,用户可以根据他收到的每一个答案投票获得信誉积分,如果他收到最佳答案,则可以获得额外积分(该代码未显示)。在User.rb
模型中,我计算了一个“业力”声誉(见下文),它只是将他获得的两种积分的总和相加。
has_reputation :karma,
:source => [
{ :reputation => :total_vote_points },
{ :reputation => :total_best_points }
]
has_reputation :total_vote_points,
:source => {reputation: :votes, of: :answers}
has_reputation :total_best_points,
:source => {reputation: :best, of: :answers}
total_vote_points 信誉将创建这样的表记录,其中 target_id:8 是用户的 id
#<ReputationSystem::Reputation id: 24, reputation_name: "total_vote_points", value: 1.0, aggregated_by: "sum", target_id: 8, target_type: "User", active: true, created_at: "2013-03-25 06:07:28", updated_at: "2013-03-25 06:07:28">
karam 声誉将创建一个像这样的表记录
#<ReputationSystem::Reputation id: 28, reputation_name: "karma", value: 11.0, aggregated_by: "sum", target_id: 7, target_type: "User", active: true, created_at: "2013-03-25 06:07:35", updated_at: "2013-03-25 06:07:35">
继续前进,gem 文档说范围可以添加到声誉中,但不要详细说明。它给出了这个例子
has_reputation :name,
...
:scopes => [:scope1, :scope2, ...]
使用我上面的代码,如果我想检查“thisWeek”和“allTime”的业力点,我猜它看起来像这样
has_reputation :karma,
:source => [
{ :reputation => :total_vote_points },
{ :reputation => :total_best_points }
],
:scopes => [:thisWeek, :allTime]
我想知道如何编写 :thisWeek (本周获得的业力积分)和 :allTime (无限制的业力积分)范围方法。
知道“业力”的一个声誉表记录看起来像这样
#<ReputationSystem::Reputation id: 28, reputation_name: "karma", value: 11.0, aggregated_by: "sum", target_id: 7, target_type: "User", active: true, created_at: "2013-03-25 06:07:35", updated_at: "2013-03-25 06:07:35">
你能建议我如何编写所需的范围方法吗?