Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow nested attributes in associations to update values in it's owne…

…r object. Fixes a regression from 3.0.x
  • Loading branch information...
commit a882790e329d3c66e640cb46e2ea19846a0c7768 1 parent 5f2bfce
Andrew Kaspick authored December 21, 2011
2  activerecord/lib/active_record/associations/association.rb
@@ -231,7 +231,7 @@ def association_class
231 231
 
232 232
         def build_record(attributes, options)
233 233
           reflection.build_association(attributes, options) do |record|
234  
-            record.assign_attributes(create_scope.except(*record.changed), :without_protection => true)
  234
+            record.assign_attributes(create_scope.except(*record.changed).merge(creation_attributes), :without_protection => true)
235 235
           end
236 236
         end
237 237
     end
3  activerecord/lib/active_record/base.rb
@@ -485,9 +485,10 @@ def initialize(attributes = nil, options = {})
485 485
 
486 486
         populate_with_current_scope_attributes
487 487
 
  488
+        yield self if block_given?
  489
+
488 490
         assign_attributes(attributes, options) if attributes
489 491
 
490  
-        yield self if block_given?
491 492
         run_callbacks :initialize
492 493
       end
493 494
 
5  activerecord/test/cases/nested_attributes_test.rb
@@ -617,6 +617,11 @@ def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_
617 617
     assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
618 618
   end
619 619
 
  620
+  def test_should_take_a_hash_with_owner_attributes_and_assign_the_attributes_to_the_associated_model
  621
+    @pirate.birds.create :name => 'bird', :pirate_attributes => {:id => @pirate.id.to_s, :catchphrase => 'Holla!'}
  622
+    assert_equal 'Holla!', @pirate.reload.catchphrase
  623
+  end
  624
+
620 625
   def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
621 626
     assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find #{@child_1.class.name} with ID=1234567890 for Pirate with ID=#{@pirate.id}" do
622 627
       @pirate.attributes = { association_getter => [{ :id => 1234567890 }] }

0 notes on commit a882790

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