File tree Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Expand file tree Collapse file tree 2 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -370,8 +370,14 @@ def validate_collection_association(reflection)
370
370
# enabled records if they're marked_for_destruction? or destroyed.
371
371
def association_valid? ( association , record )
372
372
return true if record . destroyed? || ( association . options [ :autosave ] && record . marked_for_destruction? )
373
-
374
- context = validation_context if custom_validation_context?
373
+ if custom_validation_context?
374
+ context = validation_context
375
+ else
376
+ # If the associated record is unchanged we shouldn't auto validate it.
377
+ # Even if a record is invalid you should still be able to create new references
378
+ # to it.
379
+ return true if !record . new_record? && !record . changed?
380
+ end
375
381
376
382
unless valid = record . valid? ( context )
377
383
if association . options [ :autosave ]
Original file line number Diff line number Diff line change @@ -1490,6 +1490,17 @@ def test_should_skip_validation_on_habtm_if_destroyed
1490
1490
assert_predicate @pirate , :valid?
1491
1491
end
1492
1492
1493
+ def test_should_skip_validation_on_habtm_if_persisted_and_unchanged
1494
+ parrot = @pirate . parrots . create! ( name : "parrots_1" )
1495
+ parrot . update_column ( :name , "" )
1496
+ parrot . reload
1497
+ assert_not_predicate parrot , :valid?
1498
+
1499
+ new_pirate = Pirate . new ( catchphrase : "Arr" )
1500
+ new_pirate . parrots = @pirate . parrots
1501
+ new_pirate . save!
1502
+ end
1503
+
1493
1504
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_habtm
1494
1505
@pirate . parrots . create! ( name : "parrots_1" )
1495
1506
You can’t perform that action at this time.
0 commit comments