diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 6c37d8bfba7c2492763ce9d87399d60b77ae2326..0a5a8c9713d3cf19f5bdbb62780c1aa7b96d42bf 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -317,12 +317,10 @@ def as_json(options = nil) # person.errors.to_hash # => {:name=>["cannot be nil"]} # person.errors.to_hash(true) # => {:name=>["name cannot be nil"]} def to_hash(full_messages = false) - hash = {} message_method = full_messages ? :full_message : :message - group_by_attribute.each do |attribute, errors| - hash[attribute] = errors.map(&message_method) + group_by_attribute.transform_values do |errors| + errors.map(&message_method) end - hash end def to_h @@ -348,9 +346,8 @@ def messages # Updating this hash would still update errors state for backward # compatibility, but this behavior is deprecated. def details - hash = {} - group_by_attribute.each do |attribute, errors| - hash[attribute] = errors.map(&:detail) + hash = group_by_attribute.transform_values do |errors| + errors.map(&:detail) end DeprecationHandlingDetailsHash.new(hash) end diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index 7ac49903b108e0f21978d51d9b87b89495725e4e..e40957ef3e106c8ed63b61915e1b0ac1f29aa597 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/activesupport/lib/active_support/json/decoding.rb @@ -63,8 +63,8 @@ def convert_dates_from(data) when Array data.map! { |d| convert_dates_from(d) } when Hash - data.each do |key, value| - data[key] = convert_dates_from(value) + data.transform_values! do |value| + convert_dates_from(value) end else data