Please forgive the beginner nature of this question :)
I''m trying to understand exactly where I should put specific bits of
code.
I have a Product class, with a has_many relationship with Price. Price
belongs_to Tax.
@product.price.retail gives me the retail price including tax.
@product.price.tax.rate gives me the tax rate.
I want to calculate how much of the retail price is made up of tax.
At the moment, I do this by calling
@product.calculate_tax(@product.price.retail,
@product.price.tax.rate). calculate_tax() looks like:
def calculate_tax(price, rate)
price * (rate / 100)
end
That works, but it means Ruby has to repeat the calculation each time
I want to know what the amount is. I also wondered if this should be a
helper, rather than a method in Product. However, I want to access the
tax amount elsewhere, not just in views.
So, my questions are:
Is the model the right place for this?
Can I automatically have Ruby calculate this once and then store it
somewhere like @product.tax_amount ? Is that something to put in the
Initialize method?
Thanks in advance :)
--
Matthew Revell
www.understated.co.uk