Jerome Jerome - 4 months ago 46
Ruby Question

Rails sum method returns undefined

Generating the following error

undefined method +' for #<Debit

where the controller action defines

@debits = Debit.order("vatunitid ASC").where('unit_id = ? AND folio_id = ?', session[:unit_id],
@adhoc_folios = Folio.order("created_at asc").where(['checkout IS NULL AND unit_id = ? AND id != ?', session[:unit_id],]).all
@vatunits = Vatunit.where(['unit_id = ?', session[:unit_id]]).to_a
@rates =

and presently in the view (for testing purposes)

@rates.each do |rate|
@debits_for_rate ={ |i| i.vatunitid == rate }

The count instruction returns a proper value

The call to sum is deemed at this point inexistent.

How did this come about and how to overcome it?


You are doing it in inefficient way by transforming AR into array and then performing Ruby operations while database level calculations could be used instead.

But to solve this particular issue you should go with: