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 classFoo’
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:inalias_method_chain’
from /Users/jonathan/rails/test/lib/foo_bar.rb:11:in included' from /Users/jonathan/rails/test/lib/foo_bar.rb:9:inclass_eval’
from /Users/jonathan/rails/test/lib/foo_bar.rb:9:in included' from /Users/jonathan/rails/test/app/models/foo.rb:2:ininclude’
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:inload_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:inload_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:inload_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:inconst_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

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

On Aug 7, 8:33 am, Frederick C. [email protected]

On 7 Aug 2008, at 13:19, linojon wrote:

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