Skip to content

Commit 05bef14

Browse files
authored
Merge pull request rails#33361 from jhubert/bugfix/fix-added-string-attributes
Fix regression in use of string attribute in the added? method
2 parents a8d63c2 + a199181 commit 05bef14

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

activemodel/lib/active_model/errors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def add(attribute, message = :invalid, options = {})
328328
# person.errors.added? :name, "is too long" # => false
329329
def added?(attribute, message = :invalid, options = {})
330330
if message.is_a? Symbol
331-
self.details[attribute].map { |e| e[:error] }.include? message
331+
self.details[attribute.to_sym].map { |e| e[:error] }.include? message
332332
else
333333
message = message.call if message.respond_to?(:call)
334334
message = normalize_message(attribute, message, options)

activemodel/test/cases/errors_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ def test_no_key
185185
assert person.errors.added?(:name, :blank)
186186
end
187187

188+
test "added? returns true when string attribute is used with a symbol message" do
189+
person = Person.new
190+
person.errors.add(:name, :blank)
191+
assert person.errors.added?("name", :blank)
192+
end
193+
188194
test "added? handles proc messages" do
189195
person = Person.new
190196
message = Proc.new { "cannot be blank" }

0 commit comments

Comments
 (0)