Instance variables and do loops in ruby on rails
If you work with database you can do this more efficiently with sql request:

LineItem.where(:card_id =&gt; @card.id).sum('quantity')


This is equivalent sql query

SELECT SUM(items.identity) FROM items WHERE items.card_id = ?;