mirror of https://github.com/rails/rails
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:
parent
67a838574b
commit
7f743233c4
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue