Skip to content

Commit 5f6c404

Browse files
authored
Merge pull request rails#48983 from JoeDupuis/fix-regression-on-strict-has-one-through
Fix regression blocking creation of some strict association
2 parents 40d761c + 4b35718 commit 5f6c404

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

activerecord/lib/active_record/associations/through_association.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ def construct_join_attributes(*records)
8181
# to try to properly support stale-checking for nested associations.
8282
def stale_state
8383
if through_reflection.belongs_to?
84-
Array(through_reflection.foreign_key).map do |foreign_key_column|
84+
Array(through_reflection.foreign_key).filter_map do |foreign_key_column|
8585
owner[foreign_key_column] && owner[foreign_key_column].to_s
86-
end
86+
end.presence
8787
end
8888
end
8989

activerecord/test/cases/strict_loading_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ def test_strict_loading_with_has_many_through_cascade_down_to_middle_records
284284
end
285285
end
286286

287+
def test_strict_loading_with_has_one_through_does_not_prevent_creation_of_association
288+
firm = Firm.new(name: "SuperFirm").tap(&:strict_loading!)
289+
computer = Computer.new(extendedWarranty: 1).tap(&:strict_loading!)
290+
291+
computer.firm = firm
292+
computer.developer.name = "Joe"
293+
firm.lead_developer = computer.developer
294+
295+
assert_nothing_raised do
296+
computer.save!
297+
end
298+
end
299+
300+
287301
def test_preload_audit_logs_are_strict_loading_because_parent_is_strict_loading
288302
developer = Developer.first
289303

activerecord/test/models/computer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
class Computer < ActiveRecord::Base
44
belongs_to :developer, foreign_key: "developer"
5+
has_one :firm, through: :developer
56
end

activerecord/test/models/developer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def find_most_recent
3838
accepts_nested_attributes_for :projects
3939

4040
has_and_belongs_to_many :shared_computers, class_name: "Computer"
41+
has_many :computers, foreign_key: :developer
4142

4243
has_and_belongs_to_many :projects_extended_by_name,
4344
-> { extending(ProjectsAssociationExtension) },

0 commit comments

Comments
 (0)