aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods/dirty.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-11-30 18:24:53 -0700
committerSean Griffin <sean@thoughtbot.com>2014-12-01 05:31:44 -0700
commit704c658531ae202715cba29d6b2ba64651f220fd (patch)
tree23d985b99313d0b41df014b8e29ea82f0f9a5799 /activerecord/lib/active_record/attribute_methods/dirty.rb
parent8226bad7101fd35e774ca6d233c605c4cee2abd8 (diff)
downloadrails-704c658531ae202715cba29d6b2ba64651f220fd.tar.gz
rails-704c658531ae202715cba29d6b2ba64651f220fd.tar.bz2
rails-704c658531ae202715cba29d6b2ba64651f220fd.zip
Ensure numericality validations work with mutation
The detection of in-place changes caused a weird unexpected issue with numericality validations. That validator (out of necessity) works on the `_before_type_cast` version of the attribute, since on an `:integer` type column, a non-numeric string would type cast to 0. However, strings are mutable, and we changed strings to ensure that the post type cast version of the attribute was a different instance than the before type cast version (so the mutation detection can work properly). Even though strings are the only mutable type for which a numericality validation makes sense, special casing strings would feel like a strange change to make here. Instead, we can make the assumption that for all mutable types, we should work on the post-type-cast version of the attribute, since all cases which would return 0 for non-numeric strings are immutable. Fixes #17852
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/dirty.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb12
1 files changed, 6 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 9ba46ec4c7..033e71f7b9 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -69,6 +69,11 @@ module ActiveRecord
end
end
+ def attribute_changed_in_place?(attr_name)
+ old_value = original_raw_attribute(attr_name)
+ @attributes[attr_name].changed_in_place_from?(old_value)
+ end
+
private
def calculate_changes_from_defaults
@@ -141,15 +146,10 @@ module ActiveRecord
def changed_in_place
self.class.attribute_names.select do |attr_name|
- changed_in_place?(attr_name)
+ attribute_changed_in_place?(attr_name)
end
end
- def changed_in_place?(attr_name)
- old_value = original_raw_attribute(attr_name)
- @attributes[attr_name].changed_in_place_from?(old_value)
- end
-
def original_raw_attribute(attr_name)
original_raw_attributes.fetch(attr_name) do
read_attribute_before_type_cast(attr_name)