How to find out what method_missing intercepts?

Using Ruby 1.8.6 / Rails 2.3.2

I notice that any method called for any of my ActiveRecord model classes returns nilNoMethodError instead. In addition, it is annoying, it destroys dynamic searchers ( find_by_name, find_by_idetc.), because they always return nileven where records exist. Standard classes that are not derived from ActiveRecord :: Base are not affected.

Is there any way to track what the mass_method intercepts to ActiveRecord :: Base?

UPDATE:

After switching to 1.8.7, I found (thanks to @MichaelKohl) that the will_paginate plugin handles the method_missing method. But will_paginate has existed in our system (unchanged) for quite some time, and the offender must be something later in the chain. Any ideas how to see what comes next in this chain?

UPDATE:

It turned out that there was a pearl (annotate-2.4.0) that was fixed by the monkey ActiveRecord::Base#method_missingas an empty method. Removing gem solved my problem. Although none of the answers actually found a problem, the answer from @Yanhao was the closest since it took a little tweaking to detect an alias-violating method

+5
source share
5 answers

I think @Sebi's answer is useful, but I would like to improve it as follows:

set_trace_func proc { |event, file, line, id, binding, classname|
  printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname if id.to_s == 'method_missing'
}

:

ruby-1.8.7-p334 :036 > SomeModel.some_missing_method
    call /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1873 method_missing ActiveRecord::Base
    line /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1874 method_missing ActiveRecord::Base
    line /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1874 method_missing ActiveRecord::Base
    line /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1981 method_missing ActiveRecord::Base
    line /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1998 method_missing ActiveRecord::Base
  c-call /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1998 method_missing   Kernel
   raise /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1998 method_missing ActiveRecord::Base
c-return /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1998 method_missing   Kernel
  return /Users/.../.rvm/gems/ruby-1.8.7-p334/gems/activerecord-2.3.12/lib/active_record/base.rb:1998 method_missing ActiveRecord::Base
+4

: rails

set_trace_func proc{ |event, file, line, id, binding, classname|
  printf("%8s %s:%-2d %10s %8s\n", event, file, line, id, classname) if file =~ /my_app_name/ and event == 'return' #show only interesting files
}

MyModel.non_existing_method

.

+3

TheModel.method(:method_missing).owner? Rails, :

>> class MyString < String ; end #=> nil
>> MyString.new.method(:method_missing).owner #=> BasicObject

, method_missing .

: , Ruby. @aNoble, , ? .

+2

Ruby debugger, ActiveRecord -call, nil, . , , , , , , . , , .

+2

Rails:

MyModel.method(:method_missing)

MyModel , . , method_missing. Ruby 1.9, MyModel.method(:method_missing).source_location, .

0

All Articles