我試圖計算每個月數據庫中的記錄數。但是,有個月沒有任何記錄,我想補充的到我的陣列以及當按月分組時,向陣列添加空月份
@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