# Get an attribute's value without calling the AR get method

**URL:** <https://www.ruby-forum.com/t/get-an-attributes-value-without-calling-the-ar-get-method/148386>\
**Category:** Rails\
**Created:** [September 12, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/get-an-attributes-value-without-calling-the-ar-get-method/148386 "2008-09-12T12:33:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/get-an-attributes-value-without-calling-the-ar-get-method/148386/1 "2008-09-12T12:33:42Z")

</div>

I want to override a get method to try to get a value from an associated  
object first, and then get the object’s own value if the associated  
object returned nil. But, this requires me to avoid calling a get  
method from inside the same get method.

To be specific: there are two classes, School and RegisteredInfo.  
Normally, the RegisteredInfo class holds the official data for a school

- address, postcode, phone number etc, and in most cases a school  
has\_one registered info. So, if i want to get the address for a school,  
i normally pull it out of the associated RegisteredInfo object:

address = @school.registered\_info.address

However, a school might not always have a registered info, and so has  
its own address field for this purpose. So, i want to make the address  
method as follows:

#in school.rb

def address  
if self.registered\_info  
return self.registered\_info.address  
else  
return self.address  
end  
end

However, this sets up a circular loop, where the method will keep  
calling itself. I’m sure that i’ve used a private/protected method  
called something like “read\_attribute” that pulled the value out of the  
database without calling the get method again. However, i can’t find it  
in the api (i’m using rails 2.0.2).

Can anyone help, or provide a nicer alternative to this problem?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2008, 12:41pm UTC](https://www.ruby-forum.com/t/get-an-attributes-value-without-calling-the-ar-get-method/148386/2 "2008-09-12T12:41:54Z")

</div>

On 12 Sep 2008, at 11:33, Max W. wrote:

> However, this sets up a circular loop, where the method will keep  
> calling itself. I’m sure that i’ve used a private/protected method  
> called something like “read\_attribute” that pulled the value out of  
> the  
> database without calling the get method again. However, i can’t  
> find it  
> in the api (i’m using rails 2.0.2).

read\_attribute is still there even if it’s not documented. Or you can  
use self[:address]

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 12, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/get-an-attributes-value-without-calling-the-ar-get-method/148386/3 "2008-09-12T12:46:50Z")

</div>

Frederick C. wrote:

> read\_attribute is still there even if it’s not documented. Or you can  
> use self[:address]
> 
> Fred

Fantastic - thanks Fred!
