0
<% @pid = item.producto_id %>
<br/>

Producto: <%= Producto.find_by_id(@pid) %><br/>
Costo de Compra: <%= @costo_de_compra = Producto.find(:all, :conditions => "id = '#{@pid}'").*.costo_de_compra %><br/>
Precio de venta: <%= @precio_de_venta = item.precio_de_venta %><br/>
Utilidad de este producto: <%= @precio_de_venta - @costo_de_compra %><br/><br/>
<% end %>

在我的控制器中我有=

@ventas_ocurridas_en_este_periodo = Venta.find(:all, :conditions => ['created_at >= ? and created_at <= ?', Date.today.beginning_of_month, Date.tomorrow])

当我循环并从最初不可用的项目中获取值时,我如何将从该循环中产生的所有值汇总为集中总数?

我也无法操作@precio_de_venta - @costo_de_compra ......关于数组的一些事情。

4

1 回答 1

5

我看到你的代码有几个问题。首先,您应该避免直接处理 id 列。毕竟,这就是为什么您将所有这些belongs_tohas_many关系放入模型中的原因。例如,您所说的 item 是一个 venta 或 sell 对象,它是belongs_toProduct,因为它包含一个名为 的列product_id。因此,您可以使用

item.product

代替

@pid = item.producto_id
Product.find_by_id(@pid)

更不用说find_by_id可以换成普通的了find

对于添加和查找总计,我建议您查看injectruby​​ 函数。

@costo_de_compra是一个数组,因为它来自 afind(:all)而不是 a find(:first)。我不确定你.*.是做什么的。

于 2009-11-18T22:09:31.363 回答