Skip to content

Commit 0e00fe4

Browse files
committed
Merge pull request rails#52416 from spickermann/fix-nested-error-for-singular-associations
[Rails 7.2.0.beta3] Fix bug with indexed nested error for singular associations
1 parent 92d9231 commit 0e00fe4

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

activerecord/lib/active_record/associations/association.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ def create!(attributes = nil, &block)
210210
_create_record(attributes, true, &block)
211211
end
212212

213+
# Whether the association represent a single record
214+
# or a collection of records.
215+
def collection?
216+
false
217+
end
218+
213219
private
214220
# Reader and writer methods call this so that consistent errors are presented
215221
# when the association target class does not exist.

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ def find_from_target?
313313
target.any? { |record| record.new_record? || record.changed? }
314314
end
315315

316+
def collection?
317+
true
318+
end
319+
316320
private
317321
def transaction(&block)
318322
reflection.klass.transaction(&block)

activerecord/lib/active_record/associations/nested_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(association, inner_error)
1818
def compute_attribute(inner_error)
1919
association_name = association.reflection.name
2020

21-
if index_errors_setting && index
21+
if association.collection? && index_errors_setting && index
2222
"#{association_name}[#{index}].#{inner_error.attribute}".to_sym
2323
else
2424
"#{association_name}.#{inner_error.attribute}".to_sym

activerecord/test/cases/associations/nested_error_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,43 @@ def self.name; "Guitar"; end
7474
assert_equal "is not a number", error.message
7575
assert_equal guitar, error.base
7676
end
77+
78+
class AssociationsNestedErrorWithSingularAssociationTest < ActiveRecord::TestCase
79+
def setup
80+
pet_class = Class.new(ActiveRecord::Base) do
81+
self.table_name = "pets"
82+
def self.name; "Pet"; end
83+
84+
validates :name, presence: true
85+
end
86+
87+
@owner_class = Class.new(ActiveRecord::Base) do
88+
self.table_name = "owners"
89+
def self.name; "Owner"; end
90+
91+
has_one :pet, anonymous_class: pet_class
92+
accepts_nested_attributes_for :pet
93+
validates_associated :pet
94+
end
95+
end
96+
97+
test "no index when singular association" do
98+
old_attribute_config = ActiveRecord.index_nested_attribute_errors
99+
ActiveRecord.index_nested_attribute_errors = true
100+
101+
owner = @owner_class.new(pet_attributes: { name: nil })
102+
owner.valid?
103+
104+
error = owner.errors.objects.first
105+
106+
assert_equal ActiveRecord::Associations::NestedError, error.class
107+
assert_equal owner.pet.errors.objects.first, error.inner_error
108+
assert_equal :"pet.name", error.attribute
109+
assert_equal :blank, error.type
110+
assert_equal "can't be blank", error.message
111+
assert_equal owner, error.base
112+
ensure
113+
ActiveRecord.index_nested_attribute_errors = old_attribute_config
114+
end
115+
end
77116
end

0 commit comments

Comments
 (0)