Flip your code as below:
- total_amps = @site.equipment_inventories.sum {|e| e.equipment.amp.amp }
h5 = "Total Amps: #{total_amps} amp"
- total_capacity = @site.dc_power_inventories.sum {|d| d.dc_power_supply.battery.capacity.capacity }
h5 = "Total Capcity: #{total_capacity} Amp/hr"
- capacity_left = (total_capacity / total_amps) rescue 0
h4 = "Capacity Reserve Left: #{capacity_left} Hrs "
total_capacity
and total_amps
MUST be set before using.
Currently, as total_capacity
and total_amps
are not defined there value is nil
.
Dividing nil
by nil
raises error undefined method '/' for nil:NilClass
BUT since you rescued it using rescue 0
, the output is always 0
.