diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-09-26 00:35:39 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-27 23:11:24 +0200 |
commit | 7f743233c4e276d1799451478e6718589d270cbd (patch) | |
tree | 2522008b3c309163ec135f78d59e1eac576c0e4a | |
parent | 67a838574be000f329c0f340474bc9cec1aeca16 (diff) | |
download | rails-7f743233c4e276d1799451478e6718589d270cbd.tar.gz rails-7f743233c4e276d1799451478e6718589d270cbd.tar.bz2 rails-7f743233c4e276d1799451478e6718589d270cbd.zip |
Fix for nested_attributes with has_many association fails when a single record is being updated.
[#5705 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
-rw-r--r-- | activerecord/lib/active_record/nested_attributes.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/nested_attributes_test.rb | 8 |
2 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 7372ab3278..bdd940f3ee 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -377,7 +377,12 @@ module ActiveRecord end if attributes_collection.is_a? Hash - attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes } + keys = attributes_collection.keys + attributes_collection = if keys.include?('id') || keys.include?(:id) + Array.wrap(attributes_collection) + else + attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes } + end end association = send(association_name) diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 62e073ba8c..75ffd31de3 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -123,6 +123,14 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase assert_equal 's1', ship.reload.name end + def test_has_many_association_updating_a_single_record + Man.accepts_nested_attributes_for(:interests) + man = Man.create(:name => 'John') + interest = man.interests.create(:topic => 'photography') + man.update_attributes({:interests_attributes => {:topic => 'gardening', :id => interest.id}}) + assert_equal 'gardening', interest.reload.topic + end + end class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase |