diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-06-19 01:55:33 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-06-19 01:55:50 -0300 |
commit | 2ab9ff5dfea452b0b0cf769a5ee0e91e0f77d354 (patch) | |
tree | 84c6fa90ea27e33850cfe2bc6c95c13cd9e01f83 | |
parent | 69f19b292ad4b228f2bc5f89732a8ef3b704fab6 (diff) | |
parent | 17ce0b13e66e7e0b6bc0a86f7c397453aa95729f (diff) | |
download | rails-2ab9ff5dfea452b0b0cf769a5ee0e91e0f77d354.tar.gz rails-2ab9ff5dfea452b0b0cf769a5ee0e91e0f77d354.tar.bz2 rails-2ab9ff5dfea452b0b0cf769a5ee0e91e0f77d354.zip |
Merge branch 'acapilleri-update_nested_attributes'
Closes #6675
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/dirty.rb | 20 | ||||
-rw-r--r-- | activerecord/test/cases/nested_attributes_test.rb | 10 |
2 files changed, 25 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index f8a40ad520..a85b6b3b82 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -77,11 +77,8 @@ module ActiveRecord def _field_changed?(attr, old, value) if column = column_for_attribute(attr) - 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) + if column.number? && (changes_from_nil_to_empty_string?(column, old, value) || + changes_from_zero_to_string?(column, old, value)) value = nil else value = column.type_cast(value) @@ -90,6 +87,19 @@ module ActiveRecord old != value end + + def changes_from_nil_to_empty_string?(column, old, value) + # 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) + column.null && (old.nil? || old == 0) && value.blank? + end + + def changes_from_zero_to_string?(column, old, value) + # For columns with old 0 and value non-empty string + old == 0 && value.present? && value != '0' + end end end end diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 0559bbbe9a..3daa033ed0 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -781,6 +781,16 @@ module NestedAttributesOnACollectionAssociationTests assert_nothing_raised(NoMethodError) { @pirate.save! } end + def test_numeric_colum_changes_from_zero_to_no_empty_string + Man.accepts_nested_attributes_for(:interests) + Interest.validates_numericality_of(:zine_id) + man = Man.create(:name => 'John') + interest = man.interests.create(:topic=>'bar',:zine_id => 0) + assert interest.save + + assert !man.update_attributes({:interests_attributes => { :id => interest.id, :zine_id => 'foo' }}) + end + private def association_setter |