Ruby 1.9.3-p194
Rails 3.2.8
这就是我需要的东西 计算不同的人力资源(human_resource_id)并将其除以总分配数(assignment_id)。 因此,下面给出的虚拟数据的答案应该是:
但我不知道该往哪里去。
这是我试过的:
表名:作业
id | human_resource_id | assignment_id | assignment_start_date | assignment_expected_end_date
80101780 | 20200132 | 80101780 | 2012-10-25 | 2012-10-31
80101300 | 20200132 | 80101300 | 2012-07-07 | 2012-07-31
80101308 | 21100066 | 80101308 | 2012-07-09 | 2012-07-17
首先,我需要选择我需要“看”的时间段。这总是从一年前开始。
a = Assignment.find(:all, :conditions => { :assignment_expected_end_date => (DateTime.now - 1.year)..DateTimenow })
=> [
#<Assignment id: 80101780, human_resource_id: "20200132", assignment_id: "80101780", assignment_start_date: "2012-10-25", assignment_expected_end_date: "2012-10-31">,
#<Assignment id: 80101300, human_resource_id: "20200132", assignment_id: "80101300", assignment_start_date: "2012-07-07", assignment_expected_end_date: "2012-07-31">,
#<Assignment id: 80101308, human_resource_id: "21100066", assignment_id: "80101308", assignment_start_date: "2012-07-09", assignment_expected_end_date: "2012-07-17">
]
foo = a.group_by(&:human_resource_id)
现在我得到了一个漂亮的'对象哈希数组',我不知道下一步该怎么做。
有人可以帮助我吗?
答案 0 :(得分:2)
您可以尝试在SQL中执行请求:
ActiveRecord::Base.connection.select_value('SELECT count(distinct human_resource_id) / count(distinct assignment_id) AS ratio FROM assignments');
答案 1 :(得分:1)
您可以执行类似
的操作human_resource_count = assignments.collect{|a| a.human_resource_id}.uniq.count
assignment_count = assignments.collect{|a| a.assignment_id}.uniq.count
result = human_resource_count/assignment_count