Rails 本地 ActiveRecord::NullMutationTracker:Class 的未定义局部变量或方法“first”

Rails undefined local variable or method `first' for ActiveRecord::NullMutationTracker:Class on local

当我们调用任何方法时,我们项目的所有本地设置(当前)都会出现此错误:

belongs_to :abc
after_create :some_method

def some_method
  if self.abc.saved_change_to_parent_id?
  ...
  // or even self.abc.parent_id_before_last_save
end

它给出:

NameError (undefined local variable or method `first' for ActiveRecord::NullMutationTracker:Class):

app/models/model_name.rb:50:in `some_method'

是的,没有 rails 内部文件路径等的完整跟踪,我只得到我的项目文件跟踪。也许它是一些记录器配置问题,任何获得完整跟踪的帮助也将不胜感激。

我在互联网上找不到这样的问题,所以才在这里发帖。

PS:未在 rails 问题跟踪器(github 问题)上发布,因为我没有最小复制。

堆栈

ruby: 2.4.3
rails:5.1.5(也试过 5.1.7)
OS: Ubuntu 20, 也在mac上试过OS

提前致谢。

UPDATE1: 使用 byebug,我得到了这个跟踪,错误发生的地方:

/Users/dev/.rvm/gems/ruby-2.4.3/gems/acts_as_singleton-0.0.8/lib/acts_as_singleton.rb:43

here,所以它不是直接来自 rails,而是出于某种原因我们的代码中有一个非常过时的 gem。

正如我在更新中发布的那样,问题不在于 rails 或其他任何东西,我使用 byebug 来跟踪跟踪,它类似于:

activemodel-5.1.5/lib/active_model/attribute_methods.rb:384
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:146
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:319
activerecord-5.1.5/lib/active_record/attribute_methods/dirty.rb:315
acts_as_singleton-859f49112c03/lib/acts_as_singleton.rb:43

最后一行不应该存在,因为我们没有在任何涉及的模型上使用 acts_as_singleton

所以在一些跟踪之后,发现 gem 有问题。我创建了一个叉子并使用了那个叉子。这是叉子:https://github.com/ziaulrehman40/acts_as_singleton (forked it from another fork, which seemingly had some other fixes as well). And you can see my changes here.

发生了什么问题?

这个 gem 在 ActiveRecord 模块中写入了一个名为 Singleton 的模块。这似乎没问题,除非你意识到已经有另一个名为 Singleton 的模块。 Whihc 被包含在:

gems/activerecord-5.1.5/lib/active_record/attribute_mutation_tracker.rb:83

class NullMutationTracker # :nodoc:
    include Singleton
...

正如您所见,这个 Singleton 模块被那个过时的 gem(acts_as_singleton).

覆盖(或扩展,不确定)un-intentionally