Hi,
I have a method like this, calculating the sub_total in an order model:
def sub_total result = 0 for item in self.order_items result += item.price end result end
Is there a rails way to make the method more simple and concise?
Thank you
Hi,
I have a method like this, calculating the sub_total in an order model:
def sub_total result = 0 for item in self.order_items result += item.price end result end
Is there a rails way to make the method more simple and concise?
Thank you
Hi,
I have a method like this, calculating the sub_total in an order model:
def sub_total result = 0 for item in self.order_items result += item.price end result end
Is there a rails way to make the method more simple and concise?
Have a peak at the sum method
Fred
use active record sum method
self.order_items.sum( :price )
-Sandip