Reputation: 69
I am trying to calculate a total sum from an array of ids.
It works fine when I select only one Servicio
but in an array it shows me this message:
undefined method `+' for #<Servicio:0x9c14c5c>
Extracted source (around line #91):
88: </div>
89: <div class="large-3 columns">
90: <strong><%= model_class.human_attribute_name(:total) %>:</strong></dt>
91: <%= @recibo.total %>
92: </div>
93: </div>
94: </div>
This is my model Recibo
class Recibo < ActiveRecord::Base
attr_accessible :cajero,
:doctor_id,
:numero_recibo,
:paciente_id,
:total,
:servicio_ids
belongs_to :doctor
belongs_to :paciente
has_many :atencions
has_many :servicios, :through => :atencions
def total
servicio_by_id = Servicio.find(servicio_ids)
total = servicio_by_id.sum.precio
end
end
Thanks!
Upvotes: 2
Views: 843
Reputation: 7434
You need to specify which attribute of the Servicio
should be used by the sum
method since the Servicio
class does not implement the +
method itself.
Try this
total = servicio_by_id.sum(&:precio)
This will add up the value of each Servicio
's precio
attribute.
Upvotes: 3