Skip to content

Commit 2e59171

Browse files
Merge branch '9b4a0b9-with-reverts-and-fixes' into 5813c82-with-reverts-and-fixes
2 parents 5813c82 + 267fdf4 commit 2e59171

16 files changed

+44
-179
lines changed

activerecord/lib/active_record/associations/association.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,6 @@ def extensions
188188
# not reraised. The proxy is \reset and +nil+ is the return value.
189189
def load_target
190190
@target = find_target(async: false) if (@stale_state && stale_target?) || find_target?
191-
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, through_reflection|
197-
break unless middle_target
198-
middle_target.association(through_reflection.source_reflection_name).load_target
199-
end
200-
end
201191

202192
loaded! unless loaded?
203193
target
@@ -331,10 +321,6 @@ def find_target?
331321
!loaded? && (!owner.new_record? || foreign_key_present?) && klass
332322
end
333323

334-
def set_through_target_for_new_record?
335-
owner.new_record? && reflection.through_reflection? && through_association.target
336-
end
337-
338324
# Returns true if there is a foreign key present on the owner which
339325
# references the target. This is used to determine whether we can load
340326
# the target if the owner is currently a new record (and therefore

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,6 @@ def include?(record)
272272
def load_target
273273
if find_target?
274274
@target = merge_target_lists(find_target, target)
275-
elsif target.empty? && set_through_target_for_new_record?
276-
reflections = reflection.chain.reverse!
277-
278-
@target = reflections.each_cons(2).reduce(through_association.target) do |middle_target, (middle_reflection, through_reflection)|
279-
if middle_target.nil? || (middle_reflection.collection? && middle_target.empty?)
280-
break []
281-
elsif middle_reflection.collection?
282-
middle_target.flat_map { |record| record.association(through_reflection.source_reflection_name).load_target }
283-
else
284-
middle_target.association(through_reflection.source_reflection_name).load_target
285-
end
286-
end
287275
end
288276

289277
loaded!

activerecord/lib/active_record/autosave_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def association_valid?(association, record)
376376
# If the associated record is unchanged we shouldn't auto validate it.
377377
# Even if a record is invalid you should still be able to create new references
378378
# to it.
379-
return true if !record.new_record? && !record.changed?
379+
return true if self.new_record? && !record.new_record? && !record.changed?
380380
end
381381

382382
unless valid = record.valid?(context)

activerecord/lib/active_record/reflection.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,6 @@ def source_reflection
655655
self
656656
end
657657

658-
def source_reflection_name
659-
source_reflection.name
660-
end
661-
662658
# A chain of reflections from this one back to the owner. For more see the explanation in
663659
# ThroughReflection.
664660
def collect_join_chain

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 0 additions & 79 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,
@@ -57,84 +56,6 @@ def setup
5756
Reader.create person_id: 0, post_id: 0
5857
end
5958

60-
def test_setting_has_many_through_one_association_on_new_record_sets_through_records
61-
account_1, account_2 = Account.create!(credit_limit: 100), Account.create!(credit_limit: 100)
62-
firm = Firm.new(accounts: [account_1, account_2])
63-
client = Client.new
64-
client.firm = firm
65-
66-
assert_predicate account_1, :persisted?
67-
assert_predicate account_2, :persisted?
68-
assert_predicate client, :new_record?
69-
assert_predicate client.firm, :new_record?
70-
assert_no_queries { assert_equal [account_1, account_2].sort, client.accounts.sort }
71-
end
72-
73-
def test_setting_has_many_through_many_association_on_new_record_sets_through_records
74-
subscriber_1, subscriber_2 = Subscriber.create!(nick: "nick 1"), Subscriber.create!(nick: "nick 2")
75-
subscription_1 = Subscription.new(subscriber: subscriber_1)
76-
subscription_2 = Subscription.new(subscriber: subscriber_2)
77-
book = Book.new
78-
book.subscriptions = [subscription_1, subscription_2]
79-
80-
assert_predicate subscriber_1, :persisted?
81-
assert_predicate subscriber_2, :persisted?
82-
assert_predicate book, :new_record?
83-
book.subscriptions.each { |subscription| assert_predicate subscription, :new_record? }
84-
assert_no_queries { assert_equal [subscriber_1, subscriber_2].sort, book.subscribers.sort }
85-
end
86-
87-
def test_setting_nested_has_many_through_one_association_on_new_record_sets_nested_through_records
88-
post_tagging_1, post_tagging_2 = Tagging.create!, Tagging.create!
89-
post = Post.create!(title: "Tagged", body: "Post", taggings: [post_tagging_1, post_tagging_2])
90-
author = Author.new(name: "Josh")
91-
author.posts = [post]
92-
categorization = Categorization.new
93-
categorization.author = author
94-
95-
assert_predicate post_tagging_1, :persisted?
96-
assert_predicate post_tagging_2, :persisted?
97-
assert_predicate post, :persisted?
98-
assert_predicate categorization, :new_record?
99-
assert_predicate categorization.author, :new_record?
100-
assert_no_queries { assert_equal [post_tagging_1, post_tagging_2].sort, categorization.post_taggings.sort }
101-
end
102-
103-
def test_setting_nested_has_many_through_one_association_on_new_record_sets_targetless_nested_through_records
104-
post = Post.create!(title: "Tagged", body: "Post")
105-
post_tagging_1, post_tagging_2 = Tagging.create!(taggable: post), Tagging.create!(taggable: post)
106-
author = Author.new(name: "Josh")
107-
author.posts = [post]
108-
categorization = Categorization.new
109-
categorization.author = author
110-
111-
assert_predicate post_tagging_1, :persisted?
112-
assert_predicate post_tagging_2, :persisted?
113-
assert_predicate post, :persisted?
114-
assert_predicate categorization, :new_record?
115-
assert_predicate categorization.author, :new_record?
116-
assert_queries_count(1) { assert_equal [post_tagging_1, post_tagging_2].sort, categorization.post_taggings.sort }
117-
end
118-
119-
def test_setting_nested_has_many_through_many_association_on_new_record_sets_nested_through_records
120-
account_1 = Account.create!(firm_name: "account 1", credit_limit: 100)
121-
subscriber_1 = Subscriber.create!(nick: "nick 1", account: account_1)
122-
account_2 = Account.create!(firm_name: "account 2", credit_limit: 100)
123-
subscriber_2 = Subscriber.create!(nick: "nick 2", account: account_2)
124-
subscription_1 = Subscription.new(subscriber: subscriber_1)
125-
subscription_2 = Subscription.new(subscriber: subscriber_2)
126-
book = Book.new
127-
book.subscriptions = [subscription_1, subscription_2]
128-
129-
assert_predicate subscriber_1, :persisted?
130-
assert_predicate subscriber_2, :persisted?
131-
assert_predicate account_1, :persisted?
132-
assert_predicate account_2, :persisted?
133-
assert_predicate book, :new_record?
134-
book.subscriptions.each { |subscription| assert_predicate subscription, :new_record? }
135-
assert_no_queries { assert_equal [account_1, account_2].sort, book.subscriber_accounts.sort }
136-
end
137-
13859
def test_has_many_through_create_record
13960
assert books(:awdr).subscribers.create!(nick: "bob")
14061
end

activerecord/test/cases/associations/has_one_through_associations_test.rb

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
require "models/shop_account"
2424
require "models/customer_carrier"
2525
require "models/cpk"
26-
require "models/rating"
2726

2827
class HasOneThroughAssociationsTest < ActiveRecord::TestCase
2928
fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans,
@@ -44,63 +43,6 @@ def test_has_one_through_executes_limited_query
4443
end
4544
end
4645

47-
def test_setting_association_on_new_record_sets_through_record
48-
club = Club.create!
49-
membership = CurrentMembership.new(club: club)
50-
member = Member.new
51-
member.current_membership = membership
52-
53-
assert_predicate club, :persisted?
54-
assert_predicate member, :new_record?
55-
assert_predicate member.current_membership, :new_record?
56-
assert_no_queries { assert_equal club, member.club }
57-
end
58-
59-
def test_setting_association_on_new_record_sets_nested_through_record
60-
category = Category.create!(name: "General")
61-
club = Club.create!(category: category)
62-
membership = CurrentMembership.new(club: club)
63-
member = Member.new
64-
member.current_membership = membership
65-
66-
assert_predicate category, :persisted?
67-
assert_predicate club, :persisted?
68-
assert_predicate member, :new_record?
69-
assert_predicate member.current_membership, :new_record?
70-
assert_no_queries { assert_equal club.category, member.club_category }
71-
end
72-
73-
def test_setting_association_on_new_record_sets_not_loaded_nested_through_record
74-
category = Category.create!(name: "General")
75-
club = Club.create!(category: category)
76-
club.reload
77-
membership = CurrentMembership.new(club: club)
78-
member = Member.new
79-
member.current_membership = membership
80-
81-
assert_predicate category, :persisted?
82-
assert_predicate club, :persisted?
83-
assert_predicate member, :new_record?
84-
assert_predicate member.current_membership, :new_record?
85-
assert_queries_count(1) { assert_equal club.category, member.club_category }
86-
end
87-
88-
def test_setting_association_on_new_record_sets_nested_through_record_with_belongs_to
89-
essay = Essay.create!
90-
author = Author.create!(name: "Josh", owned_essay: essay)
91-
post = Post.create!(title: "Foo", body: "Bar", author: author)
92-
comment = Comment.new(post: post)
93-
rating = Rating.new
94-
rating.comment = comment
95-
96-
assert_predicate essay, :persisted?
97-
assert_predicate author, :persisted?
98-
assert_predicate post, :persisted?
99-
assert_predicate rating, :new_record?
100-
assert_predicate rating.comment, :new_record?
101-
assert_no_queries { assert_equal essay, rating.owned_essay }
102-
end
103-
10446
def test_creating_association_creates_through_record
10547
new_member = Member.create(name: "Chris")
10648
new_member.club = Club.create(name: "LRUG")

activerecord/test/cases/autosave_association_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
require "models/tagging"
2727
require "models/treasure"
2828
require "models/eye"
29+
require "models/liquid"
2930
require "models/electron"
3031
require "models/molecule"
3132
require "models/member"
@@ -1501,6 +1502,46 @@ def test_should_skip_validation_on_habtm_if_persisted_and_unchanged
15011502
new_pirate.save!
15021503
end
15031504

1505+
def test_should_not_skip_validation_on_deeply_nested_attributes_if_unpersisted_and_unchanged
1506+
liquid = Liquid.new
1507+
molecule = liquid.molecules.build
1508+
1509+
liquid.save!
1510+
1511+
input_attributes = {
1512+
molecules_attributes: {
1513+
"0" => {
1514+
id: molecule.id,
1515+
electrons_attributes: { id: nil, name: "" }
1516+
}
1517+
}
1518+
}
1519+
1520+
assert_not liquid.update(input_attributes), "Liquid should not have been updated"
1521+
assert_includes liquid.errors.full_messages, "Molecules electrons name can't be blank"
1522+
1523+
liquid.reload
1524+
1525+
electron = molecule.electrons.build(name: "electron")
1526+
electron.save!
1527+
1528+
assert_predicate liquid, :valid?
1529+
assert_predicate electron, :persisted?
1530+
assert_predicate electron, :valid?
1531+
1532+
input_attributes = {
1533+
molecules_attributes: {
1534+
"0" => {
1535+
id: molecule.id,
1536+
electrons_attributes: { id: electron.id, name: "" }
1537+
}
1538+
}
1539+
}
1540+
1541+
assert_not liquid.update(input_attributes), "Liquid should not have been updated"
1542+
assert_includes liquid.errors.full_messages, "Molecules electrons name can't be blank"
1543+
end
1544+
15041545
def test_a_child_marked_for_destruction_should_not_be_destroyed_twice_while_saving_habtm
15051546
@pirate.parrots.create!(name: "parrots_1")
15061547

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
class FamilyTree < ActiveRecord::Base
4-
belongs_to :user
5-
64
belongs_to :member, class_name: "User", foreign_key: "member_id"
75
belongs_to :family
86
end

activerecord/test/models/liquid.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
class Liquid < ActiveRecord::Base
44
self.table_name = :liquid
55
has_many :molecules, -> { distinct }
6+
7+
accepts_nested_attributes_for :molecules
68
end

activerecord/test/models/member_detail.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class MemberDetail < ActiveRecord::Base
55
belongs_to :organization
66
has_one :member_type, through: :member
77
has_one :membership, through: :member
8-
has_one :sponsor, through: :membership
98
has_one :admittable, through: :member, source_type: "Member"
109

1110
has_many :organization_member_details, through: :organization, source: :member_details

activerecord/test/models/membership.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class Membership < ActiveRecord::Base
44
enum :type, %i(Membership CurrentMembership SuperMembership SelectedMembership TenantMembership)
55
belongs_to :member
66
belongs_to :club
7-
has_one :sponsor, through: :club
87
end
98

109
class CurrentMembership < Membership

activerecord/test/models/post.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def greeting
4646
}
4747

4848
belongs_to :author
49-
has_one :owned_essay, through: :author
5049
belongs_to :readonly_author, -> { readonly }, class_name: "Author", foreign_key: :author_id
5150

5251
belongs_to :author_with_posts, -> { includes(:posts) }, class_name: "Author", foreign_key: :author_id

activerecord/test/models/rating.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
class Rating < ActiveRecord::Base
44
belongs_to :comment
5-
has_one :post, through: :comment
6-
has_one :owned_essay, through: :post
75
has_many :taggings, as: :taggable
86
has_many :taggings_without_tag, -> { left_joins(:tag).where("tags.id": [nil, 0]) }, as: :taggable, class_name: "Tagging"
97
has_many :taggings_with_no_tag, -> { joins("LEFT OUTER JOIN tags ON tags.id = taggings.tag_id").where("tags.id": nil) }, as: :taggable, class_name: "Tagging"

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
@@ -1173,7 +1173,6 @@
11731173
t.integer :books_count, null: false, default: 0
11741174
t.integer :update_count, null: false, default: 0
11751175
t.index :nick, unique: true
1176-
t.references :account
11771176
end
11781177

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

0 commit comments

Comments
 (0)