36: Â Â Â totals[“Thursday”]+=item.hoursday4
37: Â Â Â totals[“Friday”]+=item.hoursday5
38: Â Â Â totals[“Saturday”]+=item.hoursday6
39: Â Â Â totals[“Sunday”]+=item.hoursday7
40: Â Â Â totals[“Totals”]+=item.hours
41: Â Â end
42: Â Â totals
43: Â end
Untested so may contain stupid errors, but I’d write something like
this:
 DAYS = %w( Monday Tuesday Wednesday Thursday Friday Saturday Sunday )
Â
 def totals
 totals = Hash.new(0) # default to 0 for undefined keys
Â
 items.each do |i|
 DAYS.each do |day|
 totals[day] += item.send(“hoursday#{DAYS.index(day) + 1}”)
 end
 totals[“Totals”]+=item.hours
 end
Â
 end
27: def totals
28: totals = Hash.new
29: totals[“Monday”] = totals[“Tuesday”] = totals[“Wednesday”] =
totals[“Thursday”] = totals[“Friday”] = totals[“Saturday”] =
totals[“Sunday”] = totals[“Totals”]=0 #initialise all to zero
31:
32: for item in items
33: totals[“Monday”]+=item.hoursday1
34: totals[“Tuesday”]+=item.hoursday2
35: totals[“Wednesday”]+=item.hoursday3
36: totals[“Thursday”]+=item.hoursday4
37: totals[“Friday”]+=item.hoursday5
38: totals[“Saturday”]+=item.hoursday6
39: totals[“Sunday”]+=item.hoursday7
40: totals[“Totals”]+=item.hours
41: end
42: totals
43: end
For one thing, that hash init to zero code can be much cleaner by
iterating through a DAYS array. You can also use that to trim down your
total loop.
The following code assumes the hoursdayX methods are actually database
columns, and therefore should be accessible via [field_name] notation.
If they aren’t DB columns then you need a better way to get that data
from your model.
class ItemController < ApplicationController
DAYS = [
“Monday”,
“Tuesday”,
“Wednesday”,
“Thursday”,
“Friday”,
“Satday”,
“Sunday”
]
def totals
totals = {“Totals” => 0}
DAYS.each do |day|
totals[day] = 0
end
items.each do |item|
1.upto 7 do |i|
totals[DAYS[i]] += item["hoursday#{i}"]
end
totals["Totals"] += item.hours
end
totals
34: totals[“Tuesday”]+=item.hoursday2
For one thing, that hash init to zero code can be much cleaner by
“Monday”,
DAYS.each do |day|
totals[day] = 0
end
See previous replies; it should be adequate to set the hash default to
zero:
totals = Hash.new(0)
items.each do |item|
1.upto 7 do |i|
totals[DAYS[i]] += item[“hoursday#{i}”]
You’d want DAYS[i-1] there. Or you might be able to do something with
each_with_index:
DAYS.each_with_index do |day, i|
totals[day] += item.send(“hoursday#{i+1}”)
etc.
(I’ve reverted to the send thing based on the OP’s explanation of the
constraints on the schema.)