Skip to content

Commit 8b1383b

Browse files
Revert "Handle setting of nested through records for new records"
This reverts commit 4283228.
1 parent 24bae89 commit 8b1383b

File tree

7 files changed

+7
-56
lines changed

7 files changed

+7
-56
lines changed

activerecord/lib/active_record/associations/association.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,7 @@ def extensions
189189
def load_target
190190
@target = find_target(async: false) if (@stale_state && stale_target?) || find_target?
191191
if !@target && set_through_target_for_new_record?
192-
reflections = reflection.chain
193-
reflections.pop
194-
reflections.reverse!
195-
196-
@target = reflections.reduce(through_association.target) do |middle_target, reflection|
197-
break unless middle_target
198-
middle_target.association(reflection.source_reflection_name).target
199-
end
192+
@target = through_association.target.association(reflection.source_reflection_name).target
200193
end
201194

202195
loaded! unless loaded?

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,12 @@ def load_target
273273
if find_target?
274274
@target = merge_target_lists(find_target, target)
275275
elsif target.empty? && set_through_target_for_new_record?
276-
reflections = reflection.chain
277-
reflections.pop
278-
reflections.reverse!
279-
280-
@target = reflections.reduce(through_association.target) do |middle_target, reflection|
281-
if middle_target.empty?
282-
break []
283-
elsif reflection.collection?
284-
middle_target.flat_map { |record| record.association(reflection.source_reflection_name).target }
285-
else
286-
middle_target.association(reflection.source_reflection_name).target
276+
@target = if through_reflection.collection?
277+
through_association.target.flat_map do |record|
278+
record.association(reflection.source_reflection_name).target
287279
end
280+
else
281+
through_association.target.association(reflection.source_reflection_name).target
288282
end
289283
end
290284

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
require "models/zine"
4242
require "models/interest"
4343
require "models/human"
44-
require "models/account"
4544

4645
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
4746
fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags,
@@ -68,24 +67,7 @@ def test_setting_association_on_new_record_sets_through_records
6867
assert_predicate subscriber_2, :persisted?
6968
assert_predicate book, :new_record?
7069
book.subscriptions.each { |subscription| assert_predicate subscription, :new_record? }
71-
assert_equal [subscriber_1, subscriber_2].sort, book.subscribers.sort
72-
end
73-
74-
def test_setting_association_on_new_record_sets_nested_through_records
75-
account_1 = Account.create!(firm_name: "account 1", credit_limit: 100)
76-
subscriber_1 = Subscriber.create!(nick: "nick 1", account: account_1)
77-
account_2 = Account.create!(firm_name: "account 2", credit_limit: 100)
78-
subscriber_2 = Subscriber.create!(nick: "nick 2", account: account_2)
79-
subscription_1 = Subscription.new(subscriber: subscriber_1)
80-
subscription_2 = Subscription.new(subscriber: subscriber_2)
81-
book = Book.new
82-
book.subscriptions = [subscription_1, subscription_2]
83-
84-
assert_predicate subscriber_1, :persisted?
85-
assert_predicate subscriber_2, :persisted?
86-
assert_predicate book, :new_record?
87-
book.subscriptions.each { |subscription| assert_predicate subscription, :new_record? }
88-
assert_equal [account_1, account_2].sort, book.subscriber_accounts.sort
70+
assert_equal book.subscribers.sort, [subscriber_1, subscriber_2].sort
8971
end
9072

9173
def test_has_many_through_create_record

activerecord/test/cases/associations/has_one_through_associations_test.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,6 @@ def test_setting_association_on_new_record_sets_through_record
5555
assert_equal club, member.club
5656
end
5757

58-
def test_setting_association_on_new_record_sets_nested_through_record
59-
category = Category.create!(name: "General")
60-
club = Club.create!(category: category)
61-
membership = CurrentMembership.new(club: club)
62-
member = Member.new
63-
member.current_membership = membership
64-
65-
assert_predicate category, :persisted?
66-
assert_predicate club, :persisted?
67-
assert_predicate member, :new_record?
68-
assert_predicate member.current_membership, :new_record?
69-
assert_equal club.category, member.club_category
70-
end
71-
7258
def test_creating_association_creates_through_record
7359
new_member = Member.create(name: "Chris")
7460
new_member.club = Club.create(name: "LRUG")

activerecord/test/models/book.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class Book < ActiveRecord::Base
99

1010
has_many :subscriptions
1111
has_many :subscribers, through: :subscriptions
12-
has_many :subscriber_accounts, through: :subscribers, source: :account
1312

1413
has_one :essay
1514

activerecord/test/models/subscriber.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
class Subscriber < ActiveRecord::Base
44
self.primary_key = "nick"
55

6-
belongs_to :account
7-
86
has_many :subscriptions
97
has_many :books, through: :subscriptions
108
end

activerecord/test/schema/schema.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,6 @@
11741174
t.integer :books_count, null: false, default: 0
11751175
t.integer :update_count, null: false, default: 0
11761176
t.index :nick, unique: true
1177-
t.references :account
11781177
end
11791178

11801179
create_table :subscriptions, force: true do |t|

0 commit comments

Comments
 (0)