Skip to content

Commit 5c24a73

Browse files
authored
Merge pull request rails#44843 from schlubbi/schlubbi/has-many-through-transactions
Use the through_reflection class to open transactions
2 parents 0b02ede + 1e77c84 commit 5c24a73

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

activerecord/lib/active_record/associations/through_association.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ module ThroughAssociation # :nodoc:
77
delegate :source_reflection, to: :reflection
88

99
private
10+
def transaction(&block)
11+
through_reflection.klass.transaction(&block)
12+
end
13+
1014
def through_reflection
1115
@through_reflection ||= begin
1216
refl = reflection.through_reflection

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,16 @@ def test_association_proxy_transaction_method_starts_transaction_in_association_
892892
end
893893
end
894894

895+
def test_has_many_through_uses_the_through_model_to_create_transactions
896+
post = posts(:thinking)
897+
person = people(:david)
898+
other_person = people(:michael)
899+
900+
assert_called(Reader, :transaction) do
901+
post.people = [person, other_person]
902+
end
903+
end
904+
895905
def test_has_many_association_through_a_belongs_to_association_where_the_association_doesnt_exist
896906
post = Post.create!(title: "TITLE", body: "BODY")
897907
assert_equal [], post.author_favorites

0 commit comments

Comments
 (0)