mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Autosave existing records on HMT associations when the parent is new
To me it seems like this should only be the case if `autosave: true` is set on the association. However, when implemented that way, it caused issues with has many associations, where we have explicit tests stating that child records are updated when the parent is new, even if autosave is not set (presumably to update the parent id, but other changed attributes would be persisted as well). It's quirky, but at least we should be consistently quirky. This constitutes a minor but subtle change in behavior, and therefore should not be backported to 4.2 and earlier. Fixes #19782
This commit is contained in:
parent
228559eab8
commit
d849f42b4e
3 changed files with 21 additions and 6 deletions
|
@ -1,3 +1,10 @@
|
|||
* Autosave existing records on a has many through association when the parent
|
||||
is new.
|
||||
|
||||
Fixes #19782.
|
||||
|
||||
*Sean Griffin*
|
||||
|
||||
* Fixed a bug where uniqueness validations would error on out of range values,
|
||||
even if an validation should have prevented it from hitting the database.
|
||||
|
||||
|
|
|
@ -38,13 +38,11 @@ module ActiveRecord
|
|||
def insert_record(record, validate = true, raise = false)
|
||||
ensure_not_nested
|
||||
|
||||
if record.new_record?
|
||||
if raise
|
||||
record.save!(:validate => validate)
|
||||
else
|
||||
return unless record.save(:validate => validate)
|
||||
end
|
||||
end
|
||||
|
||||
save_through_record(record)
|
||||
|
||||
|
|
|
@ -1278,6 +1278,16 @@ module AutosaveAssociationOnACollectionAssociationTests
|
|||
assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
|
||||
end
|
||||
|
||||
def test_should_update_children_when_autosave_is_true_and_parent_is_new_but_child_is_not
|
||||
parrot = Parrot.create!(name: "Polly")
|
||||
parrot.name = "Squawky"
|
||||
pirate = Pirate.new(parrots: [parrot], catchphrase: "Arrrr")
|
||||
|
||||
pirate.save!
|
||||
|
||||
assert_equal "Squawky", parrot.reload.name
|
||||
end
|
||||
|
||||
def test_should_automatically_validate_the_associated_models
|
||||
@pirate.send(@association_name).each { |child| child.name = '' }
|
||||
|
||||
|
|
Loading…
Reference in a new issue