在本地应用Rails核心修复

时间:2014-08-28 09:21:10

标签: ruby-on-rails monkeypatching

我点击了Rails 4.1.4中的一个错误,该错误最近在Rails核心修复:Fix potenital infinite recursion in changed_for_autosave? #16640

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

我已经有一个AR :: Base的扩展名,我通过ActiveRecord::Base.send(:include, ActiveRecordExtension)

从初始化程序中调用它

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

更新

配置/ intializers / 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)

1 个答案:

答案 0 :(得分:1)

我会这样写:

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