代码之家  ›  专栏  ›  技术社区  ›  rigyt

本地应用Rails核心修复

  •  1
  • rigyt  · 技术社区  · 10 年前

    我在Rails 4.1.4中遇到了一个bug,最近通过以下方式在Rails核心中修复了该bug: 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内联添加模块。

    使现代化

    config/intializers/01_extensions.rb

    require "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 回复  |  直到 10 年前
        1
  •  1
  •   wpp    10 年前

    我会这样写:

    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