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:


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

Thnx a lot

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

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:


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
(for testing only) For example:

def price

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

which doesn't work :frowning:

Any suggestions on this one ?