[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need help cleaning up a model method

Giant Cranes

8/14/2007 10:33:00 AM

Hi,

I need a little help cleaning up the method below. In my .NET days I
would have let this pass, but I am trying to be a good ruby citizen by
keeping things simple.

This is from a seller model which has many vehicles:

def total_retail_price
if @@total_retail_price.nil?
@@total_retail_price = self.vehicles.sum(:retail_price)
@@total_retail_price = 0 unless @@total_retail_price
end
@@total_retail_price
end

If I didn't have to worry about the seller having 0 vehicles I could
just use:

def total_retail_price
@@total_retail_price ||= self.vehicles.sum(:retail_price)
end

Does anyone have any suggestions on how I can fix improve on this?

Thanks,
GiantCranes
--
Posted via http://www.ruby-....

3 Answers

Giant Cranes

8/14/2007 10:39:00 AM

0

Apologies, I meant to post this in the Rails group.

--
Posted via http://www.ruby-....

Jano Svitok

8/14/2007 10:56:00 AM

0

what about this?

def total_retail_price
@@total_retail_price ||= self.vehicles.sum(:retail_price) || 0
end

Giant Cranes

8/14/2007 5:47:00 PM

0

Thanks to you both for your help
--
Posted via http://www.ruby-....