# AR: add attribute to model

**URL:** <https://www.ruby-forum.com/t/ar-add-attribute-to-model/148803>\
**Category:** Rails\
**Created:** [September 17, 2008, 8:32am UTC](https://www.ruby-forum.com/t/ar-add-attribute-to-model/148803 "2008-09-17T08:32:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [September 17, 2008, 8:32am UTC](https://www.ruby-forum.com/t/ar-add-attribute-to-model/148803/1 "2008-09-17T08:32:23Z")

</div>

Hi!

Well, I thought this was easy, but for some reason it won’t work…  
Here is what I want to do:

I am working on an API to a large database. The basic mapping is done,  
but I want to implement some convenience methods that take information  
for one object and does some calculations with it. The result should be  
accessible as attribute.

So in a simple example:

We have an object that has a start and a stop attribute, i.e. columns in  
the table. Now I want to know the distance, but directly via an readable  
attribute.

Class Something

attr\_accessor :distance

def initialize  
@distance = self.stop-self.start  
end

end

However, if I use my\_object.distance, it returns nil…

What am I doing wrong?

P.S. I also tried  
def initialize(distance) - but figured that only makes sense if I  
specifically create a new object, not if the attribute is created from  
within the class. Didn’t work either, anyway.

Cheers,

Marc

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [September 17, 2008, 8:59am UTC](https://www.ruby-forum.com/t/ar-add-attribute-to-model/148803/2 "2008-09-17T08:59:17Z")

</div>

On Sep 17, 7:32 am, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> So in a simple example:  
> @distance = self.stop-self.start  
> end

If that’s an ActiveRecord objectg, not calling super (and changing the  
signature of initialize) is a bad thing. you could avoid that  
altogether with

def distance  
@distance ||= stop - start  
end

Fred

---

<div class="post-metadata">

**Author:** ![lasastard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/58956e/32.png) [@lasastard](https://www.ruby-forum.com/u/lasastard)\
**Post date:** [September 17, 2008, 9:09am UTC](https://www.ruby-forum.com/t/ar-add-attribute-to-model/148803/3 "2008-09-17T09:09:31Z")

</div>

Frederick C. wrote:

> On Sep 17, 7:32ï¿½am, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > So in a simple example:  
> > ï¿½ @distance = self.stop-self.start  
> > ï¿½end
> 
> If that’s an ActiveRecord objectg, not calling super (and changing the  
> signature of initialize) is a bad thing. you could avoid that  
> altogether with
> 
> def distance  
> @distance ||= stop - start  
> end
> 
> Fred

_bangs head against wall_

Thanks a lot, should have thought of that myself…sometimes it’s right  
there in front of you 😉
