Ruby on rails 如何找出哪个Ruby gem劫持了ActiveRecord关联方法

Ruby on rails 如何找出哪个Ruby gem劫持了ActiveRecord关联方法,ruby-on-rails,ruby,activerecord,associations,monkeypatching,Ruby On Rails,Ruby,Activerecord,Associations,Monkeypatching,给出了这个定义(在Ruby 2.0.0-p195上使用Rails 3.2.13) 如何确定.reset的实际执行版本 更新: 当我试图调用一个不存在的方法时,我得到了这种混乱(以防这有助于解开谜团): 2.0.0-p195:052>f.recipe\u foods.snafu NoMethodError:未定义的方法“snafu”# 2.0.0-p195:053>f.配方食品.方法(:snafu) NameError:类“Array”的未定义方法“snafu” 我猜reset实际上是Associ

给出了这个定义(在Ruby 2.0.0-p195上使用Rails 3.2.13)

如何确定.reset的实际执行版本

更新:

当我试图调用一个不存在的方法时,我得到了这种混乱(以防这有助于解开谜团):

2.0.0-p195:052>f.recipe\u foods.snafu
NoMethodError:未定义的方法“snafu”#
2.0.0-p195:053>f.配方食品.方法(:snafu)
NameError:类“Array”的未定义方法“snafu”

我猜reset实际上是AssociationProxy对象上的一个方法,而不是数组,这就是为什么会得到未定义的方法。Rails 4在这方面似乎更聪明:

> c = Company.first
> c.users.method(:find)
=> # <Method: ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_User(ActiveRecord::Associations::CollectionProxy)#find>
> c.users.method(:find).source_location
=> ["/Users/me/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb", 140]
>c=Company.first
>c.users.method(:find)
=> # 
>c.users.method(:find).source\u位置
=>[“/Users/me/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active\u record/associations/collection\u proxy.rb”,140]

我不确定这里的最佳解决方案,但我想看看中的任何工具是否有帮助。

使用调试器<代码>ruby调试一直工作到ruby 1.9
byebug
适用于Ruby 2.0

在中断的调用之前编写带有调试器断点的脚本:

f = Food.last
rf = f.recipe_foods
byebug  # or debug
rf.reset
然后执行脚本。调试器将在调用之前中断,然后您可以进入调试器以了解实际执行的代码

2.0.0-p195 :040 > f.recipe_foods.method(:reset).source_location
NameError: undefined method `reset' for class `Array'
2.0.0-p195 :052 > f.recipe_foods.snafu
NoMethodError: undefined method `snafu' for #<ActiveRecord::Relation:0x007fdaef6315b0>

2.0.0-p195 :053 > f.recipe_foods.method(:snafu)
NameError: undefined method `snafu' for class `Array'
> c = Company.first
> c.users.method(:find)
=> # <Method: ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_User(ActiveRecord::Associations::CollectionProxy)#find>
> c.users.method(:find).source_location
=> ["/Users/me/.rvm/gems/ruby-2.0.0-p195/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb", 140]
f = Food.last
rf = f.recipe_foods
byebug  # or debug
rf.reset