我正在尝试计算每个月数据库中的记录数。但是有几个月没有任何记录,我也想将它们添加到我的数组中
@activity = Activity.find(params[:id])
records_by_month = @activity.records.max_12_months.group_by{ |t| t.happened.beginning_of_month }
hori = Array.new
verti = Array.new
records_by_month.sort.each do |month, recs|
hori.push(month.strftime('%B'))
verti.push(recs.count)
end
这是 records_by_month 的样子:
#<OrderedHash {Tue, 01 Jan 2013=>[#<Record id: 37, details: "",
happened: "2013-01-09", duration: nil, activity_id: 21, created_at: "2013-04-11 14:31:30",
updated_at: "2013-04-11 14:31:30", price: 15.0>], Fri, 01 Mar 2013=>
[#<Record id: 36, details: "", happened: "2013-03-04", duration: nil,
activity_id: 21, created_at: "2013-04-11 14:31:12", updated_at: "2013-04-11 14:31:12", price: 15.0>],
Thu, 01 Nov 2012=>[#<Record id: 38, details: "", happened: "2012-11-29",
duration: nil, activity_id: 21, created_at: "2013-04-11 14:31:51",
updated_at: "2013-04-11 14:31:51", price: 15.0>]}>
知道如何将月份名称添加到 hori,并将 0 添加到没有记录的每个月的 verti 吗?