Skip to content

Commit

Permalink
Do not consider the numeric attribute as changed if the old value is
Browse files Browse the repository at this point in the history
zero and the new value is not a string.

Before this commit this was the behavior

r = Review.find_by_issue(0)
r.issue
=> 0
r.changes
=> {}
r.issue = 0
=> 0
r.changed?
=> true
r.changes
=> {"issue"=>[0,0]}

Fixes #7237
  • Loading branch information
rafaelfranca committed Aug 2, 2012
1 parent 59c4b22 commit 99f622d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Rails 3.2.8 ##

* Do not consider the numeric attribute as changed if the old value is zero and the new value
is not a string.
Fixes #7237.

*Rafael Mendonça França*

* Removes the deprecation of `update_attribute`. *fxn*

* Reverted the deprecation of `composed_of`. *Rafael Mendonça França*
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ 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.present? && value != '0'
old == 0 && value.is_a?(String) && value.present? && value != '0'

This comment has been minimized.

Copy link
@jmrepetti

jmrepetti Nov 6, 2015

I had to patch this line like this in Rails 3.2.22:

old == 0 && value.is_a?(String) && value.present? && value != '0' && !(value =~ /^0\.0*$/)

because I have problems when value is '0.0'

for example:

i = Item.find(277)
i.buffer
=> #<BigDecimal:7f80becd9510,'0.0',9(18)>
i.buffer.to_s
=> "0.0"
i.buffer = '0.0'
=> "0.0"
i.changed?
=> true

Probably I have to cast the input before, not sure. I'm receiving the value '0.0' from a web form. I know Rails 4 doesn't have this problem but I can't switch to Rails 4 now.

end
end
end
Expand Down
15 changes: 14 additions & 1 deletion activerecord/test/cases/dirty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_nullable_float_not_marked_as_changed_if_new_value_is_blank
end
end

def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
def test_integer_zero_to_string_zero_not_marked_as_changed
pirate = Pirate.new
pirate.parrot_id = 0
pirate.catchphrase = 'arrr'
Expand All @@ -213,6 +213,19 @@ def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
assert !pirate.changed?
end

def test_integer_zero_to_integer_zero_not_marked_as_changed
pirate = Pirate.new
pirate.parrot_id = 0
pirate.catchphrase = 'arrr'
assert pirate.save!

assert !pirate.changed?

pirate.parrot_id = 0
assert !pirate.changed?
end


def test_zero_to_blank_marked_as_changed
pirate = Pirate.new
pirate.catchphrase = "Yarrrr, me hearties"
Expand Down

0 comments on commit 99f622d

Please sign in to comment.