Skip to content

Loading…

Decimal fields changed from zero to zero string marked as changed #8562

Closed
wants to merge 1 commit into from

7 participants

@wdgraybill

Decimal fields with an old value of 0.0 that are changed to a string representation of zero ('0.0', '0.000', etc.) are included in changed_attributes.

numeric_data = NumericData.new
numeric_data.bank_balance = 0.0
numeric_data.save!

numeric_data.bank_balance = '0.0'
numeric_data.changed_attributes # => {"bank_balance"=>#<BigDecimal:7f9f62ccefd0,'0.0',9(45)>}
@sikachu
Ruby on Rails member

@wdgraybill can you rebase your commit and force push to your branch?

/cc @tenderlove @jonleighton this seems like a valid bug to me c/d?

@wdgraybill

No problem! Rebased and force pushed.

@wdgraybill wdgraybill Fix decimal fields changed from zero to string zero
Prevent decimal fields with old value zero from being recorded as
changed when assigned to a string representing zero (e.g. '0.0')
e878a8b
@lukaszx0
Ruby on Rails member

Any news here?

@carlosantoniodasilva
Ruby on Rails member

@senny this has been fixed, confirm?

@senny
Ruby on Rails member

This is fixed indeed:

    numeric_data = NumericData.new
    numeric_data.bank_balance = 0.0
    numeric_data.save!

    numeric_data.bank_balance = '0.0'
    numeric_data.changed_attributes # => {}

closing.

@senny senny closed this
@tulak

Can somebode pleas push this change to the 3.2 rails as well ? It's impossible to use custom validations without this fix.

@bollard

+1 for pushing to 3.2

@sikachu
Ruby on Rails member

Ping @rafaelfranca, 3.2 release manager. :P

@bollard

@rafaelfranca - Any news on pushing to 3.2.x?

@senny
Ruby on Rails member

@bollard we are no longer supporting 3.2 with bugfixes.

@bollard

Should have followed this up a bit sooner! Oh well, even more reason to upgrade to Rails 4 I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2013
  1. @wdgraybill

    Fix decimal fields changed from zero to string zero

    wdgraybill committed
    Prevent decimal fields with old value zero from being recorded as
    changed when assigned to a string representing zero (e.g. '0.0')
View
5 activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Fix zero-value decimal fields registering as changed when set to a zero-value
+ string (e.g. '0.0')
+
+ *Wesley Graybill*
+
* Quote numeric values being compared to non-numeric columns. Otherwise,
in some database, the string column values will be coerced to a numeric
allowing 0, 0.0 or false to match any string starting with a non-digit.
View
6 activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -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? && !string_equal_to_zero?(value)
+ end
+
+ def string_equal_to_zero?(value)
+ Float(value) == 0 rescue false
end
end
end
View
21 activerecord/test/cases/dirty_test.rb
@@ -243,6 +243,27 @@ def test_integer_zero_to_integer_zero_not_marked_as_changed
assert !pirate.changed?
end
+ def test_decimal_zero_to_string_zero_not_marked_as_changed
+ numeric_data = NumericData.new
+ numeric_data.bank_balance = 0.0
+ assert numeric_data.save!
+
+ assert !numeric_data.changed?
+
+ numeric_data.bank_balance = '0.0'
+ assert !numeric_data.changed?
+ end
+
+ def test_decimal_zero_to_nonnumeric_string_marked_as_changed
+ numeric_data = NumericData.new
+ numeric_data.bank_balance = 0.0
+ assert numeric_data.save!
+
+ assert !numeric_data.changed?
+
+ numeric_data.bank_balance = 'arrr'
+ assert numeric_data.changed?
+ end
def test_zero_to_blank_marked_as_changed
pirate = Pirate.new
Something went wrong with that request. Please try again.