0
我正在爲我的辦公室寫一個「打卡時鐘」應用程序..我正在爲「TIme Card」視圖控制器工作,該視圖應列出給定星期的用戶拳擊次數,然後每日總分鐘數添加本週的總數。我已經想出瞭如何使用切片/貼圖獲得所有切片之間的時間差異,我的問題是,當我嘗試對有序哈希執行此操作時(按天分組),我得到了#未定義的方法`created_at',我知道我必須在某個地方丟失了一些語法,你的幫助是極大的讚賞...Slice/Map已訂購散列
這裏是我的控制器......
需要注意的是,如果我呼籲@punches @in_out_lenghts,這個工作,給我總一週,但@punches_days給了我一個錯誤,因此我無法保持運行計數....
def mytimecard
@week = params[:week].to_s
if @week == "lastweek"
@punches = Punch.lastweek.where("user_id = ?", params[:user])
else
@punches = Punch.thisweek.where("user_id = ?", params[:user])
end
@punches_days = @punches.group_by { |t| t.created_at.beginning_of_day}
if @punches.count%2 == 0
@in_out_lengths = @punches_days.each_slice(2).map { |a|(a[1].created_at).round(15.minutes) - (a[0].created_at).round(15.minutes) }
@total = ((@in_out_lengths.inject(:+))/60/60)
else
@total = "Can Not Calculate, Odd Number of Punches"
end
respond_to do |format|
format.html # timecard.html.erb
format.json { render :json => @punches }
end
end
在你的錯誤中沒有行號?我認爲這個小組在你的陣列中創建了另一個陣列。看看文檔:http://apidock.com/rails/Enumerable/group_by – phoet