1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

association load target shouldn't replace records

from db if it is already loaded by nested attributes assignment

[#5053 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Szetobo 2010-07-12 09:47:47 +08:00 committed by José Valim
parent c0bfa0bfc1
commit 0057d2df71
2 changed files with 17 additions and 4 deletions

View file

@ -396,11 +396,12 @@ module ActiveRecord
if @target.is_a?(Array) && @target.any?
@target = find_target.map do |f|
i = @target.index(f)
t = @target.delete_at(i) if i
if t && t.changed?
if i
t = @target.delete_at(i)
keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
t.attributes = f.attributes.except(*keys)
t
else
f.mark_for_destruction if t && t.marked_for_destruction?
f
end
end + @target

View file

@ -862,7 +862,19 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR
assert_equal 1, @ship.parts.proxy_target.size
assert_equal 'Deck', @ship.parts[0].name
end
test "if association is not loaded and child doesn't change and I am saving a grandchild then in memory record should be used" do
@ship.parts_attributes=[{:id => @part.id,:trinkets_attributes =>[{:id => @trinket.id, :name => 'Ruby'}]}]
assert_equal 1, @ship.parts.proxy_target.size
assert_equal 'Mast', @ship.parts[0].name
assert_no_difference("@ship.parts[0].trinkets.proxy_target.size") do
@ship.parts[0].trinkets.proxy_target.size
end
assert_equal 'Ruby', @ship.parts[0].trinkets[0].name
@ship.save
assert_equal 'Ruby', @ship.parts[0].trinkets[0].name
end
test "when grandchild changed in memory, saving parent should save grandchild" do
@trinket.name = "changed"
@ship.save