# Overriding AR accessors

**URL:** <https://www.ruby-forum.com/t/overriding-ar-accessors/82995>\
**Category:** Rails\
**Created:** [January 22, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/overriding-ar-accessors/82995 "2007-01-22T23:21:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [January 22, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/overriding-ar-accessors/82995/1 "2007-01-22T23:21:01Z")

</div>

As I said in my previous post, I need to override some AR accessor  
methods. I thought that alias\_method would work, it didn’t, I believe  
because AR methods are invoked via method\_missing. I thought  
alias\_attribute would work, but apparently that just makes a copy of the  
accessor methods, so the original and the alias both point to the same  
thing.

What I got to work was this - I need to get the object’s name, unless  
it’s nil and the object is a child in acts\_as\_tree.

def name  
if read\_attribute(:name)  
read\_attribute(:name)  
elsif read\_attribute(:parent\_id)  
self.parent.name  
end  
end

def real\_name  
read\_attribute(:name)  
end

I don’t know if I need the real\_name accessor, but either way it’s ugly  
to write out a method like the name method for each of the 10 attributes  
I need to do this with. Any suggestions would be much appreciated.

Jason

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [January 22, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/overriding-ar-accessors/82995/2 "2007-01-22T23:34:04Z")

</div>

I’ve have to be overriding a _lot_ of accessors to want to get any more  
clever than:

def name  
my\_or\_parental(:name)  
end

def nationality  
my\_or\_parental(:nationality)  
end

# …

def my\_or\_parental(attrib)  
if read\_attribute(attrib)  
read\_attribute(attrib)  
elsif self.parent\_id  
self.parent.send(attrib)  
end  
end

Ciao,  
Sheldon.

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [January 23, 2007, 1:10am UTC](https://www.ruby-forum.com/t/overriding-ar-accessors/82995/3 "2007-01-23T01:10:24Z")

</div>

Right on - I was thinking along those lines on the way home from work.  
I’ve  
never really done any programming of this type(is this  
meta-programming?),  
but I think I’m going to try for something like this:

defaults\_to\_parent :attr\_1, :attr\_2, :attr\_3

I’ll post back if I get anything in case anyone else needs something  
like  
this.

Jason

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [January 23, 2007, 1:39am UTC](https://www.ruby-forum.com/t/overriding-ar-accessors/82995/4 "2007-01-23T01:39:28Z")

</div>

Hello Jason,

> Right on - I was thinking along those lines on the way home from  
> work. I’ve never really done any programming of this type(is this  
> meta-programming?), but I think I’m going to try for something  
> like this:
> 
> defaults\_to\_parent :attr\_1, :attr\_2, :attr\_3
> 
> I’ll post back if I get anything in case anyone else needs something  
> like this.

You can write something like this :

class MyModel \< AR::B  
def self.defaults\_to\_parent(\*getters)  
getters.each do |method|  
class\_eval(\<\<-EOM, **FILE** , **LINE** )  
def #{method}  
if read\_attribute(:#{method})  
read\_attribute(:#{method})  
elsif read\_attribute(:parent\_id)  
self.parent.#{method}  
end  
end  
EOM  
end  
end

defaults\_to\_parent :attr\_1, :attr\_2, :attr\_3  
end

(not tested)

HTH,

– Jean-François.

–  
À la renverse.

---

<div class="post-metadata">

**Author:** ![quangn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/e47774/32.png) [@quangn](https://www.ruby-forum.com/u/quangn)\
**Post date:** [January 23, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/overriding-ar-accessors/82995/5 "2007-01-23T14:08:19Z")

</div>

Thank you , Jean-François. As far as I can tell, it works perfectly. If  
my object has a nil attribute, it defaults to the parent, and if the  
paren’t attribute is nil too, it returns nil. I’m not sure if I  
understand, though, what **FILE** and **LINE** do.

OK, those params are just for error reporting. I was thinking I would  
have to use eval, I didn’t know about class\_eval and it probably would  
have taken me awhile to figure it out. Your help is much appreciated.

Jason
