I find my self often doing things like this in Ruby on Rails:
@count = 0
LineItem.all.each do |item|
if (item.cart_id == @cart.id)
@count += item.quantity
end
end
So many things look wrong with this, but I want to initialize my variable (@count) before the loop, and then be able to use it after I'm out of the loop.
Is there a better way to do this?