# Adding methods to Object in Rails -- WARNING

**URL:** <https://www.ruby-forum.com/t/adding-methods-to-object-in-rails-warning/127910>\
**Category:** Rails\
**Created:** [February 2, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/adding-methods-to-object-in-rails-warning/127910 "2008-02-02T15:59:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [February 2, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/adding-methods-to-object-in-rails-warning/127910/1 "2008-02-02T15:59:02Z")

</div>

I wanted to do:

foo.wrap

and wrap would go to a specialized wrap for some types of foo and a  
general Object#wrap for all the others.

So I added wrap to Object

class Object  
def wrap  
self  
end  
end

This worked except for objects which are actually AssociationProxy  
objects pretending to be some other object. The reason is that  
AssociationProxy removes all but a few methods from itself. You will  
find this line at the top of  
vendor/rails/activerecord/lib/active\_record/associations/association\_proxy.rb

```
  instance_methods.each { |m| undef_method m unless m =~

```

/(^\_\_|^nil?$|^send$|proxy\_)/ }

This happens before the files in initializers gets loaded. And that is  
where I added Object#wrap. So, for a proxied object, it would call  
Object#wrap instead of the specialized wrap that I had defined in my  
class.

My solution to this was to do this:

module ActiveRecord  
module Associations  
class AssociationProxy  
undef\_method :wrap  
undef\_method :unwrap  
end  
end  
end

I _think_ Rails could solve this problem by hooking into  
Object.method\_added. And when methods are added, the hook could undef  
them for the proxy class(es). The other choice would be to subclass  
AssociationProxy from BlankSlate rather than Object – it appears that  
that is what BlankSlate is intended to do.
