创建范围方法

时间:2013-03-25 07:58:23

标签: ruby-on-rails

我正在玩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">

您能否建议我如何编写所需的范围方法?

0 个答案:

没有答案