Skip to content

Commit c942d3d

Browse files
authored
Merge pull request rails#29855 from lugray/has_one_destroyed_by_association
Match destroyed_by_association for has_one to has_many
2 parents 15ef55e + cb8d890 commit c942d3d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* When a `has_one` association is destroyed by `dependent: destroy`,
2+
`destroyed_by_association` will now be set to the reflection, matching the
3+
behaviour of `has_many` associations.
4+
5+
*Lisa Ugray*
6+
17
* Fix `unscoped(where: [columns])` removing the wrong bind values
28

39
When the `where` is called on a relation after a `or`, unscoping the column of that later `where` removed

activerecord/lib/active_record/associations/has_one_association.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def delete(method = options[:dependent])
5858
when :delete
5959
target.delete
6060
when :destroy
61+
target.destroyed_by_association = reflection
6162
target.destroy
6263
when :nullify
6364
target.update_columns(reflection.foreign_key => nil) if target.persisted?
@@ -80,6 +81,7 @@ def remove_target!(method)
8081
when :delete
8182
target.delete
8283
when :destroy
84+
target.destroyed_by_association = reflection
8385
target.destroy
8486
else
8587
nullify_owner_attributes(target)

activerecord/test/cases/associations/has_one_associations_test.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,38 @@ def test_association_enum_works_properly_with_nested_join
690690
SpecialAuthor.joins(book: :subscription).where.not(where_clause)
691691
end
692692
end
693+
694+
class DestroyByParentBook < ActiveRecord::Base
695+
self.table_name = "books"
696+
belongs_to :author, class_name: "DestroyByParentAuthor"
697+
before_destroy :dont, unless: :destroyed_by_association
698+
699+
def dont
700+
throw(:abort)
701+
end
702+
end
703+
704+
class DestroyByParentAuthor < ActiveRecord::Base
705+
self.table_name = "authors"
706+
has_one :book, class_name: "DestroyByParentBook", foreign_key: "author_id", dependent: :destroy
707+
end
708+
709+
test "destroyed_by_association set in child destroy callback on parent destroy" do
710+
author = DestroyByParentAuthor.create!(name: "Test")
711+
book = DestroyByParentBook.create!(author: author)
712+
713+
author.destroy
714+
715+
assert_not DestroyByParentBook.exists?(book.id)
716+
end
717+
718+
test "destroyed_by_association set in child destroy callback on replace" do
719+
author = DestroyByParentAuthor.create!(name: "Test")
720+
book = DestroyByParentBook.create!(author: author)
721+
722+
author.book = DestroyByParentBook.create!
723+
author.save!
724+
725+
assert_not DestroyByParentBook.exists?(book.id)
726+
end
693727
end

0 commit comments

Comments
 (0)