Sql ActiveRecord信誉系统相关查询(具有多个多态关联) 问题

Sql ActiveRecord信誉系统相关查询(具有多个多态关联) 问题,sql,ruby-on-rails,activerecord,polymorphic-associations,Sql,Ruby On Rails,Activerecord,Polymorphic Associations,我正在构建一个应用程序(Rails 4,mysql),以促进创新竞赛,类似于。我用它作为评级系统。我需要一个以下格式的JSON results = { contributor_count: 51 /* (submitters UNION raters).count */ opportunity_count: 100 /* total ideas submitted */ vote_count: 250 /* total votes/ratings cast */ opport

我正在构建一个应用程序(Rails 4,mysql),以促进创新竞赛,类似于。我用它作为评级系统。我需要一个以下格式的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创建/启动以下锦标赛:

“首席技术官最重要的特征是什么?”

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

“视觉”、“最喜欢的xkcd漫画”、“体验”、“粉碎兄弟技能”, “编程熟练程度”等

然后,Bob说服了50位朋友(包括大多数创意提交者)帮助他筛选提交的作品(机会),对提交的作品进行1-10分。

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

Bob将获得锦标赛的结果(见上面的JSON)

有待解决的问题 结果JSON:

  • 贡献者计数
  • 机会计数
  • 计票
  • 按平均评级的降序排列的机会
  • 评级过程

  • 随机选择用户尚未评级/投票/评估的机会
  • 投票数
  • 剩余票数

  • 状态/更新 在解决这个问题的整个过程中,我都会更新这个。期待更多信息

    贡献者计数: 我可以通过@tournaments.users获得提交者。我需要能够找到一组独特的评分员,然后将提交者与评分员联合起来。。。如何获得唯一的评分员集

    机会计数: 免费的@比赛、机会、计数

    计票: 当前的策略是@Tornament.opportunities.Injection(0){| sum,o | sum=sum+o.evaluators_for(:avg_ratings).count}

    机会按平均评级的降序排列: 六羟甲基三聚氰胺六甲醚。。。一些原始sql

    随机选择用户尚未评级/投票/评估的机会: 使用@opportunity=@Tornament.opportunities.random。。。但是,需要过滤掉那些用户没有评级的

    对计数进行投票: 与前面的问题相关@比赛。机会。在哪里(对这个机会投票)。计数

    剩余的票数: @锦标赛.机会.计数-按计数投票


    模型
    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
    
    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