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 methodfind' for class
Foo’
from /Users/jonathan/rails/test/vendor/rails/activerecord/
lib/…/…/activesupport/lib/active_support/core_ext/module/
aliasing.rb:31:inalias_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:inincluded' from /Users/jonathan/rails/test/lib/foo_bar.rb:9:in
class_eval’
from /Users/jonathan/rails/test/lib/foo_bar.rb:9:inincluded' 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