Ruby on rails 如何在每个月底计算排行榜

Ruby on rails 如何在每个月底计算排行榜,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个表格记录了一个员工在一个月内获得的投票数 [<Placement id: 1, employee_id: 1, month: "2014-02-01", votes: 2>, <Placement id: 2, employee_id: 1, month: "2014-01-01", votes: 2>, <Placement id: 3, employee_id: 2, month: "2014-02-01", votes: 1>, <Pl

我有一个表格记录了一个员工在一个月内获得的投票数

[<Placement id: 1, employee_id: 1, month: "2014-02-01", votes: 2>, 
<Placement id: 2, employee_id: 1, month: "2014-01-01", votes: 2>, 
<Placement id: 3, employee_id: 2, month: "2014-02-01", votes: 1>,
<Placement id: 4, employee_id: 2, month: "2014-01-01", votes: 3>
<Placement id: 5, employee_id: 3, month: "2014-02-01", votes: 9>]

我如何比较同一个月的选票?这会将所有月份与单个月份运行的月份进行比较,并更新您可以:

def update_single_month(month)
  Placement.where(month: month).order('votes desc').each_with_index do |placement, position|
    placement.position = position
    placement.save!
  end
end
要在所有现有数据上运行,您只需知道哪些月份已有数据:

def update_all_months
  Placement.select(:month).group(:month).each { |r| update_single_month(r.month) }
end

你可以用cron作业来完成。这个“职位”系统是如何工作的?根据什么计算职位?为什么单个员工在一个月内有多个条目?为什么可以多次出现一个位置(id为4和5的位置1)?我只想添加一个表格,其中包含一个月后的立场和投票总数。但这在很大程度上取决于实际运作的细节。一个员工在一个月内有多个条目吗?这个职位是基于每个月的多数选票
def update_single_month(month)
  Placement.where(month: month).order('votes desc').each_with_index do |placement, position|
    placement.position = position
    placement.save!
  end
end
def update_all_months
  Placement.select(:month).group(:month).each { |r| update_single_month(r.month) }
end