alias_method_chain fails

I’m getting this weird undefined method error with alias_method_chain. Same if I change find to save or whatever. It fixes if I move the alias_method_chain out of the module (see bottom of this email). What could be causing this?

models/foo.rb

class Foo < ActiveRecord::Base

include FooBar

end

lib/foo_bar.rb

module FooBar

module ClassMethods

def find_with_bar( *args ) 

  find_without_bar( *args )

end

end

def self.included(base)

base.class_eval do

  extend ClassMethods

  alias_method_chain :find, :bar

end

end

end

error

$ script/console

Loading development environment (Rails 2.0.2)

f = Foo.new

NameError: undefined method find' for class Foo’

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'

    from /Users/jonathan/rails/test/lib/foo_bar.rb:11:in `included'

    from /Users/jonathan/rails/test/lib/foo_bar.rb:9:in `class_eval'

    from /Users/jonathan/rails/test/lib/foo_bar.rb:9:in `included'

    from /Users/jonathan/rails/test/app/models/foo.rb:2:in `include'

    from /Users/jonathan/rails/test/app/models/foo.rb:2

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:203:in `load_without_new_constant_marking'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:203:in `load_file'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:202:in `load_file'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:94:in `require_or_load'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:248:in `load_missing_constant'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:453:in `const_missing'

    from /Users/jonathan/rails/test/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/dependencies.rb:465:in `const_missing'

    from (irb):1

THIS WAY WORKS

models/foo.rb

class Foo < ActiveRecord::Base

include FooBar

class << self

alias_method_chain :find, :bar

end

end

lib/foo_bar.rb

module FooBar

module ClassMethods

def find_with_bar( *args ) 

  find_without_bar( *args )

end

end

def self.included(base)

base.class_eval do

  extend ClassMethods

  #alias_method_chain :find, :bar

end

end

end

I'm getting this weird undefined method error with
alias_method_chain. Same if I change find to save or whatever. It
fixes if I move the alias_method_chain out of the module (see
bottom of this email). What could be causing this?

basically because you're not calling alias_method_chain on the right
class:

  def self.included(base)
    base.class_eval do
      extend ClassMethods
      alias_method_chain :find, :bar
    end
  end

Is the same as you doing

class Foo < AR:Base
   alias_method_chain :find, :bar
end

which obviously doesn't work

base.class_eval do
   extend ClassMethods
   class << self
      alias_method_chain :find, :bar
   end
  end
end

should

thanks, that did it
blogged at http://www.vaporbase.com/postings/alias_method_chain_in_a_module