In nested_attributes when association is not loaded and association

record is saved and then in memory record attributes should be saved

[#5053 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Subba Rao Pasupuleti
2010-07-10 12:29:09 -04:00
committed by José Valim
parent 6ba7d5e654
commit c0bfa0bfc1
2 changed files with 14 additions and 1 deletions

View File

@@ -477,7 +477,14 @@ module ActiveRecord
callback(:before_add, record)
yield(record) if block_given?
@target ||= [] unless loaded?
@target << record unless @reflection.options[:uniq] && @target.include?(record)
index = @target.index(record)
unless @reflection.options[:uniq] && index
if index
@target[index] = record
else
@target << record
end
end
callback(:after_add, record)
set_inverse_instance(record, @owner)
record

View File

@@ -856,6 +856,12 @@ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveR
@part = @ship.parts.create!(:name => "Mast")
@trinket = @part.trinkets.create!(:name => "Necklace")
end
test "if association is not loaded and association record is saved and then in memory record attributes should be saved" do
@ship.parts_attributes=[{:id => @part.id,:name =>'Deck'}]
assert_equal 1, @ship.parts.proxy_target.size
assert_equal 'Deck', @ship.parts[0].name
end
test "when grandchild changed in memory, saving parent should save grandchild" do
@trinket.name = "changed"