1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fixing bug on ActiveRecord::Dirty#field_changed? for nullable numeric columns, NULL gets stored in database for blank (i.e. '') values. Only integer columns were considered.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1692 state:committed]
This commit is contained in:
Carlos Kozuszko 2009-01-04 20:22:53 -02:00 committed by Michael Koziarski
parent 9bcf01b23c
commit c891d685de
2 changed files with 3 additions and 3 deletions

View file

@ -151,8 +151,8 @@ module ActiveRecord
def field_changed?(attr, old, value) def field_changed?(attr, old, value)
if column = column_for_attribute(attr) if column = column_for_attribute(attr)
if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank? if column.number? && column.null && (old.nil? || old == 0) && value.blank?
# For nullable integer columns, NULL gets stored in database for blank (i.e. '') values. # For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
# Hence we don't record it as a change if the value changes from nil to ''. # Hence we don't record it as a change if the value changes from nil to ''.
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll # If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
# be typecast back to 0 (''.to_i => 0) # be typecast back to 0 (''.to_i => 0)

View file

@ -58,7 +58,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal parrot.name_change, parrot.title_change assert_equal parrot.name_change, parrot.title_change
end end
def test_nullable_integer_not_marked_as_changed_if_new_value_is_blank def test_nullable_number_not_marked_as_changed_if_new_value_is_blank
pirate = Pirate.new pirate = Pirate.new
["", nil].each do |value| ["", nil].each do |value|