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>
This commit is contained in:
Neeraj Singh 2010-09-26 00:35:39 -04:00 committed by José Valim
parent 67a838574b
commit 7f743233c4
2 changed files with 14 additions and 1 deletions

View File

@ -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)

View File

@ -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