Skip to content

Commit

Permalink
Merge pull request #9042 from senny/9034_float_0_0_is_always_dirty
Browse files Browse the repository at this point in the history
Assigning '0.0' to a nullable numeric column does not make it dirty

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] })
  • Loading branch information
carlosantoniodasilva committed Mar 5, 2013
2 parents 5cf472e + 4b7a33e commit 09d1fb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions 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:

Expand Down
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -107,7 +107,11 @@ def changes_from_nil_to_empty_string?(column, old, value)

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
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Expand Up @@ -243,6 +243,21 @@ def test_integer_zero_to_integer_zero_not_marked_as_changed
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
Expand Down

0 comments on commit 09d1fb2

Please sign in to comment.