我试图计算每个月数据库中的记录数。但是,有个月没有任何记录,我想补充的到我的阵列以及当按月分组时,向阵列添加空月份
@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?
你可以给'records_by_month'线的输出? – 2013-04-11 19:41:28
我更新了我的文章 – networkprofile 2013-04-11 20:05:12
为什么要添加'0'?需要这些信息。 – 2013-04-11 20:25:32