1

问题

我正在构建一个应用程序(Rails 4,mysql)来促进创新比赛,类似于Darwinator。我使用ActiveRecord 信誉系统作为评级系统。我需要以下格式的 JSON

results = 
{
  contributor_count: 51  /* (submitters UNION raters).count */
  opportunity_count: 100 /* total ideas submitted */
  vote_count: 250 /* total votes/ratings cast */
  opportunities: [  /* descending order of avg_rating */
    {avg_rating: 9.6, content: "vision"},
    {avg_rating: 9,   content: "favorite xkcd comic"},
    {avg_rating: 8,   content: "smash bro skill"},
    {avg_rating: 7.9, content: "programming proficiency"},
    {avg_rating: 7.1, content: "experience"},
    ...
  ] 
}


用户故事

Bob 创建/开始以下锦标赛:

“CTO 最重要的特征是什么?”

Bob 说服他的 20 位朋友提交总共 100 个想法(机会):

“视觉”、“最喜欢的xkcd漫画”、“经验”、“smash bro技能”、“编程熟练度”等。

然后 Bob 说服他的 50 位朋友(包括大多数想法提交者)帮助他通过按 1 到 10 的等级对提交进行评分来过滤提交(机会)。

在对 100 个提交的机会进行 250 次评分后(平均每个机会 2.5 次评分),Bob 单击“完成比赛”按钮,并被带到比赛结果页面

Bob 将看到比赛的结果(参见上面的 JSON )


需要解决的问题

结果 JSON:

  1. 贡献者计数
  2. 机会计数
  3. 投票计数
  4. 按 avg_rating 降序排列的机会

评级过程

  1. 随机选择用户未评分/投票/评估的机会
  2. voted_on_count
  3. 剩余投票数


状态更新

我将在解决这个问题的整个过程中更新它。期待更多信息。

贡献者计数:我可以通过@tournaments.users 获得提交者。我需要能够对唯一的评估者集,然后将提交者与评估者联合起来......我怎样才能获得唯一的评估者集?

机会计数:免费赠品。@tournament.opportunities.count

vote_count : 当前策略是@tournament.opportunities.inject(0) {|sum, o| sum = sum + o.evaluators_for(:avg_ratings).count }

按 avg_rating 降序排列的机会:嗯……一些原始 sql?

随机选择用户未评分/投票/评估的机会:使用ramdumb gem,@opportunity = @tournament.opportunities.random... 但是,需要过滤掉用户未评分的机会

voted_on_count:与上一个问题有关。@tournament.opportunities.where(已对此机会投票).count

remaining_to_vote_on_count : @tournament.opportunities.count - voted_on_count


楷模

class Tournament < ActiveRecord::Base
  has_many :opportunities
  # submitters of
  has_many :users, through: :opportunities
  ...
end

class Opportunity < ActiveRecord::Base
  belongs_to :tournament
  belongs_to :user

  has_reputation :avg_rating,
                 source: :user,
                 aggregated_by: :average
  ...
end

class User < ActiveRecord::Base
  has_many :opportunities
  has_many :tournaments, through: :opportunities
  has_many :evaluations, class_name: "RSEvaluation", as: :source

  has_reputation :avg_rating,
                 source: {reputation: :avg_rating, of: :opportunities},
                 aggregated_by: :average
  ...
end

架构.rb

create_table "tournaments", force: true do |t|
  t.boolean  "currently_running", default: true
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "description"
end

create_table "users", force: true do |t|
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "opportunities", force: true do |t|
  t.string   "content"
  t.integer  "tournament_id"
  t.integer  "user_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "rs_evaluations", force: true do |t|
  t.string   "reputation_name"
  t.integer  "source_id"
  t.string   "source_type"
  t.integer  "target_id"
  t.string   "target_type"
  t.float    "value",           default: 0.0
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "rs_reputations", force: true do |t|
  t.string   "reputation_name"
  t.float    "value",           default: 0.0
  t.string   "aggregated_by"
  t.integer  "target_id"
  t.string   "target_type"
  t.boolean  "active",          default: true
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "rs_reputation_messages", force: true do |t|
  t.integer  "sender_id"
  t.string   "sender_type"
  t.integer  "receiver_id"
  t.float    "weight",      default: 1.0
  t.datetime "created_at"
  t.datetime "updated_at"
end
4

0 回答 0