From 9890e3f075d1f6284b5cb332cfa247d8d0c0905c Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 27 Sep 2010 17:42:05 -0400 Subject: [PATCH] backport of #5705 to 3-0-stable [#5705 state:resolved] --- .../lib/active_record/nested_attributes.rb | 7 ++++++- activerecord/test/cases/nested_attributes_test.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 520969a..f5cbc47 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -349,7 +349,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 9823d7a..e35f30b 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -114,6 +114,15 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase pirate.ship_attributes = { :name => 'Hello Pearl' } assert_difference('Ship.count') { pirate.save! } 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 -- 1.7.2