# Reflecting on an Association Extension

**URL:** <https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938>\
**Category:** Rails\
**Created:** [October 22, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938 "2007-10-22T16:38:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938/1 "2007-10-22T16:38:39Z")

</div>

Hi all.

How can I reflect on a method which had been extended into an  
association.

If i try model.association.method(:method\_name) I get an undefined  
error yet I can invoke it via model.association.method\_name. I’ve  
tried several different approaches to get at the method but can’t seem  
to be able to get hold of it.

Thanks  
Garrett

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938/2 "2007-10-22T17:31:56Z")

</div>

On 22 Oct 2007, at 15:37, garrett wrote:

> 

class Post  
has\_many :comments do  
def some\_method  
end  
end  
end

The basic issues is that post.comments is not an array, it’s an  
instance of a proxy class that will (when needed) load the collection  
and forward methods.  
so when you call post.comments.method(:some\_method) that’s being  
forwarded on to the array itself.

You can use post.comments.proxy\_respond\_to?(:foo) to find if the  
proxy itself response to a method, but the ‘method’ method has been  
undef’d on association proxies.

Fred.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938/3 "2007-10-22T17:51:53Z")

</div>

Thanks Fred

Is there no way then for me to check that two method names actually  
have the same implementation.

I had been intending the test that

class Post  
has\_many :comments do  
def foo  
end  
alias\_method :bar, :foo  
end  
end

post.comments.method(:foo) == post.comments.method(:bar)

On Oct 22, 4:30 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938/4 "2007-10-22T19:18:49Z")

</div>

garrett wrote:

> Thanks Fred
> 
> Is there no way then for me to check that two method names actually  
> have the same implementation.
> 
> I had been intending the test that
> 
> class Post  
> has\_many :comments do  
> def foo  
> end  
> alias\_method :bar, :foo  
> end  
> end
> 
> post.comments.method(:foo) == post.comments.method(:bar)

The #method() method in Ruby isn’t very helpful for comparing  
implementations:

> > “foo”.method(:length) == “foo”.method(:length)  
> > =\> false

If you need to check to see if an extension has been included in an  
association, you could grope through the ancestor modules. Or it may be  
easier to define a duck-type predicate in the extension module, like  
#is\_my\_fancy\_type?, that you can use to check if the association has  
that type.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 22, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/reflecting-on-an-association-extension/117938/5 "2007-10-22T19:24:28Z")

</div>

Josh S. wrote:

> garrett wrote:
> 
> > Thanks Fred
> > 
> > Is there no way then for me to check that two method names actually  
> > have the same implementation.
> > 
> > I had been intending the test that
> > 
> > class Post  
> > has\_many :comments do  
> > def foo  
> > end  
> > alias\_method :bar, :foo  
> > end  
> > end
> > 
> > post.comments.method(:foo) == post.comments.method(:bar)
> 
> The #method() method in Ruby isn’t very helpful for comparing  
> implementations:
> 
> > > “foo”.method(:length) == “foo”.method(:length)  
> > > =\> false
> 
> If you need to check to see if an extension has been included in an  
> association, you could grope through the ancestor modules. Or it may be  
> easier to define a duck-type predicate in the extension module, like  
> #is\_my\_fancy\_type?, that you can use to check if the association has  
> that type.

Ah, oops. Ruby’s per-object methods get me again. Method identity is  
unique per instance. The two “foo” strings in my other example are of  
course different objects. Sigh. Symbols are the same instance though,  
therefore:

> > :foo.method(:to\_s) == :foo.method(:to\_s)  
> > =\> true

So if you have the exact same association instance, you could actually  
compare implementations of messages to see if they were the same method.  
I still don’t get the use case though.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
