# Need to change attr-value when model is loaded

**URL:** <https://www.ruby-forum.com/t/need-to-change-attr-value-when-model-is-loaded/144293>\
**Category:** Rails\
**Created:** [July 28, 2008, 11:43am UTC](https://www.ruby-forum.com/t/need-to-change-attr-value-when-model-is-loaded/144293 "2008-07-28T11:43:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 28, 2008, 11:43am UTC](https://www.ruby-forum.com/t/need-to-change-attr-value-when-model-is-loaded/144293/1 "2008-07-28T11:43:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 28, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/need-to-change-attr-value-when-model-is-loaded/144293/2 "2008-07-28T12:36:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 28, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/need-to-change-attr-value-when-model-is-loaded/144293/3 "2008-07-28T13:14:20Z")

</div>

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](http://www.therailsway.com/2006/11/16/tracks-part-2)

Thanks,  
Kiran,  
[http://kiran.gnufied.org](http://kiran.gnufied.org)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 28, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/need-to-change-attr-value-when-model-is-loaded/144293/4 "2008-07-28T14:19:57Z")

</div>

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 ☹

Any suggestions on this one ?
