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

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?

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

Frederick C. wrote:

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

Fred

Fantastic - thanks Fred!