提交 0057d2df 编写于 作者: S Szetobo 提交者: José Valim

association load target shouldn't replace records

from db if it is already loaded by nested attributes assignment

[#5053 state:resolved]
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 c0bfa0bf
......@@ -396,11 +396,12 @@ def load_target
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
......
......@@ -862,7 +862,19 @@ def setup
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册