提交 f6b21d48 编写于 作者: G Godfrey Chan

Merge pull request #16640 from mfazekas/fix_loop_in_changed_for_autosave

Fix potenital infinite recursion in changed_for_autosave?
......@@ -278,11 +278,18 @@ def associated_records_to_validate_or_save(association, new_record, autosave)
# go through nested autosave associations that are loaded in memory (without loading
# any new ones), and return true if is changed for autosave
def nested_records_changed_for_autosave?
self.class._reflections.values.any? do |reflection|
if reflection.options[:autosave]
association = association_instance_get(reflection.name)
association && Array.wrap(association.target).any?(&:changed_for_autosave?)
@_nested_records_changed_for_autosave_already_called ||= false
return false if @_nested_records_changed_for_autosave_already_called
begin
@_nested_records_changed_for_autosave_already_called = true
self.class._reflections.values.any? do |reflection|
if reflection.options[:autosave]
association = association_instance_get(reflection.name)
association && Array.wrap(association.target).any?(&:changed_for_autosave?)
end
end
ensure
@_nested_records_changed_for_autosave_already_called = false
end
end
......
......@@ -1030,6 +1030,16 @@ def test_should_automatically_save_the_associated_model
assert_equal 'The Vile Insanity', @pirate.reload.ship.name
end
def test_changed_for_autosave_should_handle_cycles
@ship.pirate = @pirate
assert_queries(0) { @ship.save! }
@parrot = @pirate.parrots.create(name: "some_name")
@parrot.name="changed_name"
assert_queries(1) { @ship.save! }
assert_queries(0) { @ship.save! }
end
def test_should_automatically_save_bang_the_associated_model
@pirate.ship.name = 'The Vile Insanity'
@pirate.save!
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册