From f11560afa1e55fe1d4c8c4b81081a2b4af988992 Mon Sep 17 00:00:00 2001 From: Adrianna Chang Date: Thu, 16 Jan 2025 10:39:01 -0500 Subject: [PATCH] Revert "[Fix #33155] Set through target for new records" This reverts commit bec10dc86bced7a2325db391672eccc6676614ca. --- .../lib/active_record/associations/association.rb | 7 ------- .../associations/collection_association.rb | 8 -------- .../has_many_through_associations_test.rb | 14 -------------- .../has_one_through_associations_test.rb | 12 ------------ 4 files changed, 41 deletions(-) diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index cbbf690f2032f..7b634d698e10f 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -188,9 +188,6 @@ def extensions # not reraised. The proxy is \reset and +nil+ is the return value. def load_target @target = find_target(async: false) if (@stale_state && stale_target?) || find_target? - if !@target && set_through_target_for_new_record? - @target = through_association.target.association(reflection.source_reflection_name).target - end loaded! unless loaded? target @@ -324,10 +321,6 @@ def find_target? !loaded? && (!owner.new_record? || foreign_key_present?) && klass end - def set_through_target_for_new_record? - owner.new_record? && reflection.through_reflection? && through_association.target - end - # Returns true if there is a foreign key present on the owner which # references the target. This is used to determine whether we can load # the target if the owner is currently a new record (and therefore diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 5e85af7b49a24..bfef699e47657 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -272,14 +272,6 @@ def include?(record) def load_target if find_target? @target = merge_target_lists(find_target, target) - elsif target.empty? && set_through_target_for_new_record? - @target = if through_reflection.collection? - through_association.target.flat_map do |record| - record.association(reflection.source_reflection_name).target - end - else - through_association.target.association(reflection.source_reflection_name).target - end end loaded! diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 825763f7ebec7..30ee65a67781e 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -56,20 +56,6 @@ def setup Reader.create person_id: 0, post_id: 0 end - def test_setting_association_on_new_record_sets_through_records - subscriber_1, subscriber_2 = Subscriber.create!(nick: "nick 1"), Subscriber.create!(nick: "nick 2") - subscription_1 = Subscription.new(subscriber: subscriber_1) - subscription_2 = Subscription.new(subscriber: subscriber_2) - book = Book.new - book.subscriptions = [subscription_1, subscription_2] - - assert_predicate subscriber_1, :persisted? - assert_predicate subscriber_2, :persisted? - assert_predicate book, :new_record? - book.subscriptions.each { |subscription| assert_predicate subscription, :new_record? } - assert_equal book.subscribers.sort, [subscriber_1, subscriber_2].sort - end - def test_has_many_through_create_record assert books(:awdr).subscribers.create!(nick: "bob") end diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index bfc9ebef572c7..e674bcd5a87f5 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -43,18 +43,6 @@ def test_has_one_through_executes_limited_query end end - def test_setting_association_on_new_record_sets_through_record - club = Club.create! - membership = CurrentMembership.new(club: club) - member = Member.new - member.current_membership = membership - - assert_predicate club, :persisted? - assert_predicate member, :new_record? - assert_predicate member.current_membership, :new_record? - assert_equal club, member.club - end - def test_creating_association_creates_through_record new_member = Member.create(name: "Chris") new_member.club = Club.create(name: "LRUG")