मैंने 4.1.4 रेल में एक बग मारा, जो हाल ही में रेल कोर में तय किया गया है: Change_for_autosave में पोटेनिटल अनंत पुनरावृत्ति को ठीक करें? # 16,640
मैं अस्थायी रूप से इस फिक्स में स्थानीय रूप से पैच कर सकता हूं जब तक कि मेरे पास एक रेल संस्करण में अपग्रेड न हो, जिसमें यह है? एक्टिवरेकार्ड / lib / active_record / autosave_association.rb में एक परिवर्तन है
मेरे पास पहले से ही AR :: Base के लिए एक एक्सटेंशन है जिसे मैं एक इनिशियलाइज़र से कॉल करता हूं जिसमें स्वयं के माध्यम से शामिल है ActiveRecord::Base.send(:include, ActiveRecordExtension)
मैंने बदली हुई विधि के साथ ऑटोसैविएशन के लिए उस फाइल में एक मॉड्यूल इनलाइन जोड़ने की कोशिश की।
अद्यतन करें
config / 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 के लिए 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