gpt4 book ai didi

ruby-on-rails - 模型 rails 计算总价

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:47 25 4
gpt4 key购买 nike

我正在尝试从一组 ID 中计算总和。当我只选择一个 Servicio 时它工作正常但在一个数组中它向我显示这条消息:

    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>

这是我的模特 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

谢谢!

最佳答案

您需要指定 sum 方法应该使用 Servicio 的哪个属性,因为 Servicio 类没有实现 >+ 方法本身。

试试这个

total = servicio_by_id.sum(&:precio)

这会将每个 Servicioprecio 属性的值相加。

关于ruby-on-rails - 模型 rails 计算总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com