在本地应用Rails核心修复程序



我在Rails 4.1.4中遇到了一个错误,最近在Rails核心中修复了这个错误:修复changed_for_autosave中的潜在无限递归#16640

在升级到具有此功能的Rails版本之前,我如何在本地临时修补此修复程序?修复程序是对activerecord/lib/active_record/autosave_association.rb 的更改

我已经有一个AR::Base的扩展,我通过ActiveRecord::Base.send(:include, ActiveRecordExtension) 从包含它自己的初始化器调用它

我尝试用更改后的方法在该文件中为AutosaveAssociation添加一个内联模块。

更新

config/initializers/01_extensions.rb

require "active_record_extension"

active_record_extension.rb

module ActiveRecordExtension
  extend ActiveSupport::Concern
  module ClassMethods
  ...
  end
  module AutosaveAssociation
    def nested_records_changed_for_autosave?
      return false if @_nested_records_changed_for_autosave_already_called ||= false
      @_nested_records_changed_for_autosave_already_called = true
      begin
        self.class._reflections.values.any? do |reflection|
          if reflection.options[:autosave]
            association = association_instance_get(reflection.name)
            association && Array.wrap(association.target).any? { |a| a.changed_for_autosave? }
          end
        end
      ensure
        @_nested_records_changed_for_autosave_already_called = false
      end
    end
  end # module Autosave
end
ActiveRecord::Base.send(:include, ActiveRecordExtension)

我会这样写:

module ActiveRecord#Extension
  module AutosaveAssociation
    def nested_records_changed_for_autosave?
      puts "new"
    end
  end
end
#ActiveRecord::Base.send(:include, ActiveRecordExtension)
class A < ActiveRecord::Base
  include ActiveRecord::AutosaveAssociation
  def initialize
    nested_records_changed_for_autosave?
  end
end
A.new
=> new

最新更新