Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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>
  • Loading branch information...
commit c0bfa0bfc17f4aa615cd9d1006509e0d84b5692d 1 parent 6ba7d5e
@subbarao subbarao authored josevalim committed
View
9 activerecord/lib/active_record/associations/association_collection.rb
@@ -477,7 +477,14 @@ def add_record_to_target_with_callbacks(record)
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
6 activerecord/test/cases/nested_attributes_test.rb
@@ -856,6 +856,12 @@ def setup
@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"

0 comments on commit c0bfa0b

Please sign in to comment.
Something went wrong with that request. Please try again.