diff options
author | Yves Senn <yves.senn@gmail.com> | 2013-01-23 10:57:05 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2013-03-05 14:00:41 +0100 |
commit | 4b7a33e1423c207b25da7e34d8ea45c71ba12298 (patch) | |
tree | 92b26475a696f6915f83dbcc03a0a9cfa45714d2 | |
parent | ee444697073b123bbf0e36511b581f7df7419636 (diff) | |
download | rails-4b7a33e1423c207b25da7e34d8ea45c71ba12298.tar.gz rails-4b7a33e1423c207b25da7e34d8ea45c71ba12298.tar.bz2 rails-4b7a33e1423c207b25da7e34d8ea45c71ba12298.zip |
assigning '0.0' to a nullable numeric column does not make it dirty
-rw-r--r-- | activerecord/CHANGELOG.md | 12 | ||||
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/dirty_test.rb | 15 |
3 files changed, 32 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f08049a443..97616ffc58 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,17 @@ ## Rails 4.0.0 (unreleased) ## +* Assigning "0.0" to a nullable numeric column does not make it dirty. + Fix #9034. + + Example: + + product = Product.create price: 0.0 + product.price = '0.0' + product.changed? # => false (this used to return true) + product.changes # => {} (this used to return { price: [0.0, 0.0] }) + + *Yves Senn* + * Added functionality to unscope relations in a relations chain. For instance, if you are passed in a chain of relations as follows: diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 616ae1631f..6315dd9549 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -107,7 +107,11 @@ module ActiveRecord def changes_from_zero_to_string?(old, value) # For columns with old 0 and value non-empty string - old == 0 && value.is_a?(String) && value.present? && value != '0' + old == 0 && value.is_a?(String) && value.present? && non_zero?(value) + end + + def non_zero?(value) + value !~ /\A0+(\.0+)?\z/ end end end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index c7d2ba6073..7b2034dadf 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -243,6 +243,21 @@ class DirtyTest < ActiveRecord::TestCase assert !pirate.changed? end + def test_float_zero_to_string_zero_not_marked_as_changed + data = NumericData.new :temperature => 0.0 + data.save! + + assert_not data.changed? + + data.temperature = '0' + assert_empty data.changes + + data.temperature = '0.0' + assert_empty data.changes + + data.temperature = '0.00' + assert_empty data.changes + end def test_zero_to_blank_marked_as_changed pirate = Pirate.new |