views:

109

answers:

1

hi i have this :

<% for item in @ventas_ocurridas_en_este_periodo do %>

<% @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 %>

and in my controller i have=

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

As i am looping and getting values from the items that aren't available originally how can i sum all the values that come off from this loop into a concentred total?

also i can't make the operation of @precio_de_venta - @costo_de_compra... something about arrays.

+3  A: 

I see several issues with your code. First, you should avoid dealing with id columns directly. After all, that's why you put all those belongs_to and has_many relations into your models. For example, what you call item is a venta or sell object, and it belongs_to Product as it includes a column named product_id. Therefore, you can use

item.product

instead of

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

not to mention that find_by_id can be replaced by just a regular find.

For adding and finding totals, I recommend you to look into the inject ruby function.

@costo_de_compra is an array as it comes from a find(:all) instead of a find(:first). I am not sure about what your .*. do.

gdelfino