# Two alias\_method\_chain calls: first works second doesn't

**URL:** <https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635>\
**Category:** Rails\
**Created:** [April 2, 2008, 6:24am UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635 "2008-04-02T06:24:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 2, 2008, 6:24am UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/1 "2008-04-02T06:24:19Z")

</div>

In the example code in this Pastie [Parked at Loopia](http://pastie.caboo.se/129000), I’m  
observing behavior I can’t explain. I’m making two calls to  
alias\_method\_chain to extend create\_table and drop\_table. The  
create\_table alias chain works as expected. While debugging the  
drop\_table\_with alias chained method is never entered.

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 2, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/2 "2008-04-02T16:10:46Z")

</div>

Any chance it’s as simple as the placement of your requires?

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 3, 2008, 4:35am UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/3 "2008-04-03T04:35:14Z")

</div>

On Apr 2, 10:09 am, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Thanks in advance for your help.  
> > I don’t think so. Both create\_table\_with and drop\_table\_with are in  
> > the same file which is included from environment.rb’s last line

ActiveRecord::ConnectionAdapters::SchemaStatements.send(:include,  
AuditingTables::ActiveRecord::ConnectionAdapters::SchemaStatements)

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 3, 2008, 5:09am UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/4 "2008-04-03T05:09:19Z")

</div>

Okay,

I haven’t dug deep into this but I can give you a few pointers.

First of all, the alias\_method\_chain is being done to a module so your  
methods only do stuff when they are actually included in a class.

With that in mind, what happens here?

module Failure  
def failure\_type  
“everything is fine”  
end

def failure\_type\_with\_foo  
puts “I intercepted it!”  
failure\_type\_without\_foo  
end  
alias\_method\_chain :failure\_type, :foo  
end

class SomethingThatFails  
include Failure  
def failure\_type  
“epic”  
end  
end

SomethingThatFails.new.failure\_type #=\> “epic”

This could be an explanation.

Regards,  
Trevor

On 4/2/08, Jobu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > drop\_table\_with alias chained method is never entered.
> 
> >

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 7, 2008, 1:58am UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/5 "2008-04-07T01:58:02Z")

</div>

On Apr 3, 10:14 am, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thought they might provide something helpful.  
> drop\_table\_with\_auditing\_table doesn’t require the code in the  
> requires. Also, that function’s not even being entered.

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 3, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/6 "2008-04-03T16:15:47Z")

</div>

Sorry I should have been more specific. Your pastie shows:

line 13: def drop\_table\_with\_auditing\_table

line 20 require ‘rails\_generator’  
line 21 require ‘rails\_generator/scripts/generate’

line 23 def create\_table\_with\_auditing\_table

Since the code that works follows the requires at lines 20 and 21 I  
thought they might provide something helpful.

On a side note, depending on what your audit code needs to accomplish  
“acts\_as\_versioned” might be a good fit.

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 7, 2008, 2:01am UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/7 "2008-04-07T02:01:33Z")

</div>

Thanks for looking, but I believe I’ve tried what you suggest.

1. I believe the extension to the module is included in the extended  
module globally, because of the following line in environment.rb  
ActiveRecord::ConnectionAdapters::SchemaStatements.send(:include,AuditingTables::ActiveRecord::ConnectionAdapters::SchemaStatements)

2. I’ve used a debugger and seen that the failure\_type\_with\_foo (my  
drop\_table\_with\_auditing\_table) in your example is never entered.  
However my create\_table\_with\_auditing\_table, which is defined and  
included in an identical manner is always entered.

Could something be overriding or blocking my extension of drop\_table?

---

<div class="post-metadata">

**Author:** ![jobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea5d25/32.png) [@jobu](https://www.ruby-forum.com/u/jobu)\
**Post date:** [April 7, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/two-alias-method-chain-calls-first-works-second-doesnt/133635/8 "2008-04-07T17:14:42Z")

</div>

On 4/6/08, Jobu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. I’ve used a debugger and seen that the failure\_type\_with\_foo (my  
> drop\_table\_with\_auditing\_table) in your example is never entered.  
> However my create\_table\_with\_auditing\_table, which is defined and  
> included in an identical manner is always entered.
> 
> Could something be overriding or blocking my extension of drop\_table?

Yeah, that’s the whole point of what I showed you. No amount of  
monkeypatching in the Failure module can compensate for the fact that  
the SomethingThatFails class defines its own failure\_type() method.

Try to figure out the type of the object you are calling drop\_table()  
on, then look at the rails source to see whether that class in  
question happens to define its own drop\_table method.

If you’re impatient you can always just search for “def drop\_table” in  
the rails source.

Chances are, you’re going to have to monkeypatch that class rather  
than SchemaStatements.

HTH  
Trevor

## –

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
