diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-05-20 20:50:46 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-05-20 20:53:47 +0100 |
commit | 281edce6db8accc7d4a0e9ab01892631d9d0ebc3 (patch) | |
tree | 6d56e7bae4636d1cab9da18c8880880206c2bfa1 /activerecord/lib | |
parent | 73c59638549686fccc749ffd3ac53cb533c5fd61 (diff) | |
download | rails-281edce6db8accc7d4a0e9ab01892631d9d0ebc3.tar.gz rails-281edce6db8accc7d4a0e9ab01892631d9d0ebc3.tar.bz2 rails-281edce6db8accc7d4a0e9ab01892631d9d0ebc3.zip |
Ensure nil to '' doesn't get recorded by dirty for nullable integer columns. [#150 state:resolved] [Jason Dew, Pratik]
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/dirty.rb | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index 6034963811..49f1204fa1 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -117,14 +117,7 @@ module ActiveRecord # The attribute already has an unsaved change. unless changed_attributes.include?(attr) old = clone_attribute_value(:read_attribute, attr) - - # Remember the original value if it's different. - typecasted = if column = column_for_attribute(attr) - column.type_cast(value) - else - value - end - changed_attributes[attr] = old unless old == typecasted + changed_attributes[attr] = old if field_changed?(attr, old, value) end # Carry on. @@ -138,5 +131,20 @@ module ActiveRecord update_without_dirty end end + + def field_changed?(attr, old, value) + if column = column_for_attribute(attr) + if column.type == :integer && column.null && old.nil? + # For nullable integer 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 ''. + value = nil if value.blank? + else + value = column.type_cast(value) + end + end + + old != value + end + end end |