Skip to content

Commit

Permalink
Revert "[Fix rails#33155] Set through target for new records"
Browse files Browse the repository at this point in the history
This reverts commit bec10dc.
  • Loading branch information
adrianna-chang-shopify committed Jan 17, 2025
1 parent 262bdc1 commit f11560a
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 41 deletions.
7 changes: 0 additions & 7 deletions activerecord/lib/active_record/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f11560a

Please sign in to comment.