2013-04-11 51 views
0

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

@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?

+0

你可以給'records_by_month'線的輸出? – 2013-04-11 19:41:28

+0

我更新了我的文章 – networkprofile 2013-04-11 20:05:12

+0

爲什麼要添加'0'?需要這些信息。 – 2013-04-11 20:25:32

回答

1

好的(稍做改動以便測試)......這樣的事情呢?

ALL = [%w{ 
    January February March 
    April May  June 
    July August September 
    October November December 
}, [0] * 12].transpose 
hori = Hash[ALL] 
verti = Hash[ALL] 
{'January' => 1, 'March' => 5}.each do |month, recs| 
    hori[month] = :test 
    verti[month] = recs 
end 
p hori.sort.map(&:last) # => [0, 0, 0, 0, :test, 0, 0, :test, 0, 0, 0, 0] 
p verti.sort.map(&:last) # => [0, 0, 0, 0, 1, 0, 0, 5, 0, 0, 0, 0] 
+0

請問您可以添加一些意見到底發生了什麼?我猜我應該用'records_by_month'替換'{'January'=> 1,'March'=> 5}',但我不太瞭解代碼。 – networkprofile 2013-04-11 20:07:49

+0

你是對的。這個想法是,以每個月爲一個關鍵點很容易。 '[0] * 12'和'.transpose'只是在每個月後添加一個任意類型的值(在這種情況下爲0),這與寫作完全相同:'{'January'=> 0,'February'= > 0,...}'。由於我們構造的是Hash對象而不是數組,因此我們不需要對'records_by_month'進行排序,並且在可用鍵的值被設置後(對於'horti'和'verti'),只需要通過映射每個鍵散列(實際上,它們是數組之後)通過Symbol的#to_proc實例方法通過'element.last'實現。 (:last) – DigitalRoss 2013-04-11 20:56:37

+0

所以,是的,你需要用'records_by_month'替換'{...}',你可能還必須從block參數'month'中提取真實的月份字符串,並且稍微改變賦值。 – DigitalRoss 2013-04-11 21:00:37

0

這是你的問題的一個提示,而不是完整的解決方案:

require 'date' 

h = {"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 = {} 
verti = {} 

h.each {|k,v| hori[Date.parse(k).strftime('%B')] = v} 
p hori 

Date::MONTHNAMES.compact.map {|m| verti[m]=0 unless hori.keys.include? m} 
p verti 

輸出:

{"January"=>[{: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}], "March"=>[{: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}], "November"=>[{: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}]} 

{"February"=>0, "April"=>0, "May"=>0, "June"=>0, "July"=>0, "August"=>0, "September"=>0, "October"=>0, "December"=>0}