Redefining the method belongs_to in ActiveRecord::Base

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 moduleActiveRecord’ (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

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 | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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.