Skip to content

Commit

Permalink
Fixing bug on ActiveRecord::Dirty#field_changed? for nullable numeric…
Browse files Browse the repository at this point in the history
… 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]
  • Loading branch information
ckozus authored and NZKoz committed Jan 15, 2009
1 parent 5664f24 commit 9606bc8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/dirty.rb
Expand Up @@ -151,8 +151,8 @@ def update_with_dirty

def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank?
# For nullable integer columns, NULL gets stored in database for blank (i.e. '') values.
if column.number? && column.null && (old.nil? || old == 0) && value.blank?
# 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 ''.
# 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)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/dirty_test.rb
Expand Up @@ -58,7 +58,7 @@ def test_aliased_attribute_changes
assert_equal parrot.name_change, parrot.title_change
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

["", nil].each do |value|
Expand Down

0 comments on commit 9606bc8

Please sign in to comment.