Skip to content

Commit e94e6c2

Browse files
committed
Revert "Merge pull request rails#8313 from alan/only_save_changed_has_one_objects"
This reverts commit 6e3ab3e, reversing changes made to 39e07b6. Conflicts: activerecord/CHANGELOG.md activerecord/test/cases/autosave_association_test.rb
1 parent 9a976ab commit e94e6c2

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Save `has_one` association even if the record doesn't changed.
2+
3+
Fixes #14407.
4+
5+
*Rafael Mendonça França*
6+
17
* Use singular table name in generated migrations when
28
`ActiveRecord::Base.pluralize_table_names` is `false`.
39

@@ -76,12 +82,6 @@
7682

7783
*Troy Kruthoff*, *Lachlan Sylvester*
7884

79-
* Only save has_one associations if record has changes.
80-
Previously after save related callbacks, such as `#after_commit`, were triggered when the has_one
81-
object did not get saved to the db.
82-
83-
*Alan Kennedy*
84-
8585
* Allow strings to specify the `#order` value.
8686

8787
Example:

activerecord/lib/active_record/autosave_association.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,15 @@ def save_collection_association(reflection)
377377
def save_has_one_association(reflection)
378378
association = association_instance_get(reflection.name)
379379
record = association && association.load_target
380-
381380
if record && !record.destroyed?
382381
autosave = reflection.options[:autosave]
383382

384383
if autosave && record.marked_for_destruction?
385384
record.destroy
386-
elsif autosave != false
385+
else
387386
key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
387+
if autosave != false && (autosave || new_record? || record_changed?(reflection, record, key))
388388

389-
if (autosave && record.changed_for_autosave?) || new_record? || record_changed?(reflection, record, key)
390389
unless reflection.through_reflection
391390
record[reflection.foreign_key] = key
392391
end

activerecord/test/cases/autosave_association_test.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -683,23 +683,10 @@ def save(*args)
683683
end
684684
end
685685

686-
@ship.pirate.catchphrase = "Changed Catchphrase"
687-
688686
assert_raise(RuntimeError) { assert !@pirate.save }
689687
assert_not_nil @pirate.reload.ship
690688
end
691689

692-
def test_should_save_changed_has_one_changed_object_if_child_is_saved
693-
@pirate.ship.name = "NewName"
694-
assert @pirate.save
695-
assert_equal "NewName", @pirate.ship.reload.name
696-
end
697-
698-
def test_should_not_save_changed_has_one_unchanged_object_if_child_is_saved
699-
@pirate.ship.expects(:save).never
700-
assert @pirate.save
701-
end
702-
703690
# belongs_to
704691
def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
705692
assert !@ship.pirate.marked_for_destruction?

0 commit comments

Comments
 (0)