# Alias\_method\_chain causing infinite recursion and stack level too deep

**URL:** <https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228>\
**Category:** Rails\
**Created:** [January 20, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228 "2009-01-20T21:49:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Law](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@David\_Law](https://www.ruby-forum.com/u/David_Law)\
**Post date:** [January 20, 2009, 9:49pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228/1 "2009-01-20T21:49:21Z")

</div>

I installed the Facebooker plugin and upon placing it on a staging  
server, I keep running into stack level too deep errors. It seems to  
stem from Facebooker using alias\_method\_chain to override some of the  
ActionController methods. I believe it’s getting loaded twice causing  
confusion. I’ve tried various methods include wrapping a unless  
respond\_to?(:method) around the alias\_method\_chain call to keep it  
from being called twice. However, none of this has worked? Has  
anyone run into this problem and found a remedy? I know  
alias\_method\_chain is a pretty popular way to override methods, so I  
can’t imagine this being the first instance of alias\_method\_chain  
causing this problem. Here is some of the sample code I’m looking to  
fix:

module ::ActionController  
class Base  
def self.inherited\_with\_facebooker(subclass)  
inherited\_without\_facebooker(subclass)  
if subclass.to\_s == “ApplicationController”  
subclass.send(:include,Facebooker::Rails::Controller)  
subclass.helper Facebooker::Rails::Helpers  
end  
end  
class \<\< self  
unless ActionController::Base.respond\_to?  
(:inherited\_with\_facebooker)  
alias\_method\_chain :inherited, :facebooker  
end

```
end

```

end  
end

Here you can see the method inherited has been aliased to  
inherited\_with\_facebooker and inherited\_without\_facebooker has been  
aliased to inherited. However, when the method is called  
inherited\_without\_facebooker goes into an endless recursive call which  
results in a stack level too deep error. I tried to prevent  
alias\_method\_chain from being called twice (which is what I suspect  
the problem being) by checking if inherited\_with\_facebooker already  
exists. It has not seemed to fix the problem. Does anyone else have  
a possible solution I may try. Thanks again!

---

<div class="post-metadata">

**Author:** ![David\_Law](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@David\_Law](https://www.ruby-forum.com/u/David_Law)\
**Post date:** [January 20, 2009, 9:52pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228/2 "2009-01-20T21:52:55Z")

</div>

On 20 Jan 2009, at 20:48, David Law wrote:

> 

> a possible solution I may try. Thanks again!

You’re in the right mindset but you haven’t got you’re check quite  
right. You ask whether the object ActionController::Base (ie the  
class) responds to that method and it doesn’t so that will always  
return false. What you want is whether instances of that class  
respond\_to your method (so this is the same as  
String.respond\_to? :strip returning false, “”.respond\_to? :strip  
returns true)

Fred

---

<div class="post-metadata">

**Author:** ![David\_Law](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@David\_Law](https://www.ruby-forum.com/u/David_Law)\
**Post date:** [January 20, 2009, 10:26pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228/3 "2009-01-20T22:26:29Z")

</div>

Initially I had self.respond\_to? but it did not work either which lead  
me to believe that it might be a class problem instead of an instance  
one. Is that what you are referring to?

On Jan 20, 12:52 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![David\_Law](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@David\_Law](https://www.ruby-forum.com/u/David_Law)\
**Post date:** [January 20, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228/4 "2009-01-20T22:52:24Z")

</div>

On 20 Jan 2009, at 21:19, David Law wrote:

> Initially I had self.respond\_to? but it did not work either which lead  
> me to believe that it might be a class problem instead of an instance  
> one. Is that what you are referring to?

Not really. What I am saying is that SomeClass.respond\_to? :foo  
tells you whether you can do SomeClass.foo, not whether you can do  
some\_instance.foo

Fred

---

<div class="post-metadata">

**Author:** ![David\_Law](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@David\_Law](https://www.ruby-forum.com/u/David_Law)\
**Post date:** [January 20, 2009, 11:43pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228/5 "2009-01-20T23:43:06Z")

</div>

On 20 Jan 2009, at 22:28, David Law wrote:

> I understand that. I’m not particularly familiar with class \<\< self  
> but it seems that everything within its bounds is considered a  
> instance method. In this case would I simply put respond\_to?  
> (:inherited\_with\_facebooker)? Sorry to be bugging you so frequently,  
> just trying to understand this as best as I can. Thank you for all  
> your help.

I’d check WhateverClass.method\_defined?

Fred

---

<div class="post-metadata">

**Author:** ![David\_Law](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/53a042/32.png) [@David\_Law](https://www.ruby-forum.com/u/David_Law)\
**Post date:** [January 20, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/alias-method-chain-causing-infinite-recursion-and-stack-level-too-deep/158228/6 "2009-01-20T23:29:02Z")

</div>

I understand that. I’m not particularly familiar with class \<\< self  
but it seems that everything within its bounds is considered a  
instance method. In this case would I simply put respond\_to?  
(:inherited\_with\_facebooker)? Sorry to be bugging you so frequently,  
just trying to understand this as best as I can. Thank you for all  
your help.

David

On Jan 20, 1:51 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
