need to change attr-value when model is loaded

He All

I have a situation in which I store all prices excl VAT. Now, this module needs to have the price incl VAT. How do I do this, for example, in the controllers I do something like

my_model = Xyz.find(:first) .... my_model.price ....

I don't want to call (in all the controllers) a function like:

my_model.convert_price

The 'my_model' should already have the price incl VAT. How do I do this in the model ?

Thnx a lot LuCa

define the vat amount as const somewhere in environment.rb like VAT = 1.19 (not necessary, but better is that...)

then just define a method price_vat like

def price_vat   price * VAT end

Hi Luca,

You can try defining the method in application.rb and use before_filter macro to declare it in controllers. Would like to know more about filters? Refer this: http://www.therailsway.com/2006/11/16/tracks-part-2

Thanks, Kiran, http://kiran.gnufied.org

I found an other very interesting way to do this (although it doesn't work yet)

just re-define the 'price' method

The question I have is: how do I create the same price method as activeRecord. (for testing only) For example:

def price   read_attribute(:price) end

gives me problems (I get nil object etc), so ActiveRecord does more then this. Furthermore, is there a way to call the original price method, I tried something like

def price   super end

which doesn't work :frowning:

Any suggestions on this one ?