提交 c0ef95a1 编写于 作者: S Sean Griffin

Correctly ignore `mark_for_destruction` without `autosave`

As per the docs, `mark_for_destruction` should do nothing if `autosave`
is not set to true. We normally persist associations on a record no
matter what if the record is a new record, but we were always skipping
records which were `marked_for_destruction?`.

Fixes #20882
上级 9bd6e39b
* Correctly ignore `mark_for_destruction` when `autosave` isn't set to `true`
when validating associations.
Fixes #20882.
*Sean Griffin*
* Fix a bug where counter_cache doesn't always work with polymorphic
relations.
......
......@@ -325,7 +325,7 @@ def validate_collection_association(reflection)
# the parent, <tt>self</tt>, if it wasn't. Skips any <tt>:autosave</tt>
# enabled records if they're marked_for_destruction? or destroyed.
def association_valid?(reflection, record)
return true if record.destroyed? || record.marked_for_destruction?
return true if record.destroyed? || (reflection.options[:autosave] && record.marked_for_destruction?)
validation_context = self.validation_context unless [:create, :update].include?(self.validation_context)
unless valid = record.valid?(validation_context)
......
......@@ -1154,6 +1154,13 @@ def save(*args)
def test_should_not_load_the_associated_model
assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
end
def test_mark_for_destruction_is_ignored_without_autosave_true
ship = ShipWithoutNestedAttributes.new(name: "The Black Flag")
ship.parts.build.mark_for_destruction
assert_not ship.valid?
end
end
class TestAutosaveAssociationOnAHasOneThroughAssociation < ActiveRecord::TestCase
......
......@@ -22,6 +22,7 @@ def cancel_save_callback_method
class ShipWithoutNestedAttributes < ActiveRecord::Base
self.table_name = "ships"
has_many :prisoners, inverse_of: :ship, foreign_key: :ship_id
has_many :parts, class_name: "ShipPart", foreign_key: :ship_id
validates :name, presence: true
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册