# Redefining the method belongs\_to in ActiveRecord::Base

**URL:** <https://www.ruby-forum.com/t/redefining-the-method-belongs-to-in-activerecord-base/75373>\
**Category:** Rails\
**Created:** [November 9, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/redefining-the-method-belongs-to-in-activerecord-base/75373 "2006-11-09T14:26:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vinicius\_Cubas\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e95f7d/32.png) [@Vinicius\_Cubas\_B](https://www.ruby-forum.com/u/Vinicius_Cubas_B)\
**Post date:** [November 9, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/redefining-the-method-belongs-to-in-activerecord-base/75373/1 "2006-11-09T14:26:51Z")

</div>

Hi. I’ve tried to redefine the method belongs\_to in ActiveRecord::Base,  
so every time a model is defined as belonging to :attachment, it should  
have some additional methods. I tried to do an alias of belongs\_to, but  
this was not successful. The error message in Webrick was

> Booting WEBrick…  
> ./script/…/config/…/lib/ActiveRecordExtensions.rb:67: undefined  
> method `belongs_to' for module`ActiveRecord’ (NameError)

The code with problem is:

alias :attachment\_belongs\_to :belongs\_to  
def belongs\_to(association\_id, options={})  
self.attachment\_belongs\_to(association\_id,options)  
if association\_id == :attachment

```
 code = <<EOS

```

alias :tmp\_attach= :attachment=  
def attachment=(content)  
if content.class.name == ‘Attachment’ #old functionality  
self.tmp\_attach=(content)  
else  
if content.class.name.match(/^Hash/) && content[“content”] &&  
content[“content”].original\_filename != “”  
if self.attachment  
self.attachment.update\_attributes(content)  
else  
attach = Attachment.new(content)  
attach.save  
self.tmp\_attach=(attach)  
end  
end  
end  
end

alias :tmp\_destroy :destroy  
def destroy  
attach = self.attachment  
self.tmp\_destroy  
attach.destroy if attach  
end  
EOS  
module\_eval(code)

```
end

```

end

---

<div class="post-metadata">

**Author:** ![Vinicius\_Cubas\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e95f7d/32.png) [@Vinicius\_Cubas\_B](https://www.ruby-forum.com/u/Vinicius_Cubas_B)\
**Post date:** [November 9, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/redefining-the-method-belongs-to-in-activerecord-base/75373/2 "2006-11-09T14:32:53Z")

</div>

Hi –

On Thu, 9 Nov 2006, Vinicius Cubas B. wrote:

> The code with problem is:
> 
> alias :attachment\_belongs\_to :belongs\_to

I suspect the problem was actually before this. It sounds like you  
had:

module ActiveRecord

when you wanted:

class ActiveRecord::Base

(I haven’t looked at the rest of the code in detail but I think that’s  
what’s triggering this error.)

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Vinicius\_Cubas\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e95f7d/32.png) [@Vinicius\_Cubas\_B](https://www.ruby-forum.com/u/Vinicius_Cubas_B)\
**Post date:** [November 9, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/redefining-the-method-belongs-to-in-activerecord-base/75373/3 "2006-11-09T17:52:04Z")

</div>

unknown wrote:

> Hi –
> 
> On Thu, 9 Nov 2006, Vinicius Cubas B. wrote:
> 
> > The code with problem is:
> > 
> > alias :attachment\_belongs\_to :belongs\_to
> 
> I suspect the problem was actually before this. It sounds like you  
> had:
> 
> module ActiveRecord
> 
> when you wanted:
> 
> class ActiveRecord::Base
> 
> (I haven’t looked at the rest of the code in detail but I think that’s  
> what’s triggering this error.)

I suspect this is correct. Also belongs\_to is a class method and must  
be declared as such. You can also define you own class that  
enscapsulates this through inheritance, which has the advantage of  
calling to original functionality through a clean ans slick call to  
“super”

class Foo \< ActiveRecord::Base  
self.abstract\_class = true # tell AR there is no table for this  
class  
def self.belongs\_to(\*args)  
if args[0] == ‘something’  
super(\*args) # do whatever AR does normally  
else  
do\_something\_custom  
end  
end  
end

class MyModel \< Foo  
belongs\_to :bar  
end

In my experience inheritance is the cleanest way to extend an existing  
complex class with non standard functionality and just feels more right  
than some method aliasing hack around a method.
