【问题标题】:How to figure out which Ruby gem has hijacked an ActiveRecord association method如何找出哪个 Ruby gem 劫持了 ActiveRecord 关联方法
【发布时间】:2013-08-25 10:07:26
【问题描述】:

鉴于此定义(在 Ruby 2.0.0-p195 上使用 Rails 3.2.13)...

class Food < ActiveRecord::Base
    has_many :recipe_foods, foreign_key: :food_id

.reset 没有按照文档记录(它应该重置 @loaded 标志,而是重新查询数据库并返回结果)...

2.0.0-p195 :037 > f = Food.last
  Food Load (1.6ms) ...
 => #<Food ...

2.0.0-p195 :038 > f.recipe_foods
  RecipeFood Load (9.4ms) ...
 => [#<RecipeFood ...

2.0.0-p195 :039 > f.recipe_foods.reset
  RecipeFood Load (10.0ms) ...
 => [#<RecipeFood ...

我怀疑其他一些 gem 劫持了该方法,但这是我从 .method 得到的...

2.0.0-p195 :040 > f.recipe_foods.method(:reset).source_location
NameError: undefined method `reset' for class `Array'

我如何确定实际执行的是哪个版本的 .reset?

更新:

当我尝试调用一个不存在的方法时,我会陷入混乱(以防这有助于解开谜团):

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'

【问题讨论】:

    标签: ruby-on-rails ruby activerecord associations monkeypatching


    【解决方案1】:

    我的猜测是,reset 实际上是 AssociationProxy 对象上的一个方法,而不是 Array,这就是你得到未定义方法的原因。 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]
    

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

    【讨论】:

      【解决方案2】:

      使用调试器。 ruby-debug 一直工作到 Ruby 1.9; byebug 适用于 Ruby 2.0。

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

      f = Food.last
      rf = f.recipe_foods
      byebug  # or debug
      rf.reset
      

      然后执行脚本。调试器将在调用之前中断,然后您可以单步执行以找出实际正在执行的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2011-06-06
        • 2019-03-18
        • 1970-01-01
        相关资源
        最近更新 更多