Need help cleaning up a model method

G

Giant Cranes

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
 
J

Jano Svitok

what about this?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top