Skip to content
Browse files

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]
  • Loading branch information...
1 parent 9bcf01b commit c891d685de9a729332836751c1293770b86a1b52 Carlos Kozuszko committed with NZKoz
Showing with 3 additions and 3 deletions.
  1. +2 −2 activerecord/lib/active_record/dirty.rb
  2. +1 −1 activerecord/test/cases/dirty_test.rb
View
4 activerecord/lib/active_record/dirty.rb
@@ -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)
View
2 activerecord/test/cases/dirty_test.rb
@@ -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|

0 comments on commit c891d68

Please sign in to comment.
Something went wrong with that request. Please try again.