diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/nested_attributes.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/nested_attributes_test.rb | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 6718b4a69d..eb9e792dd8 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -355,7 +355,7 @@ module ActiveRecord association.to_a else attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact - attribute_ids.present? ? association.all(:conditions => {:id => attribute_ids}) : [] + attribute_ids.present? ? association.all(:conditions => {association.primary_key => attribute_ids}) : [] end attributes_collection.each do |attributes| diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index eae8ae7e39..fadd62b5a1 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -6,6 +6,8 @@ require "models/parrot" require "models/treasure" require "models/man" require "models/interest" +require "models/owner" +require "models/pet" require 'active_support/hash_with_indifferent_access' module AssertRaiseWithMessage @@ -707,3 +709,26 @@ class TestNestedAttributesLimit < ActiveRecord::TestCase end end end + +class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase + fixtures :owners, :pets + + def setup + Owner.accepts_nested_attributes_for :pets + + @owner = owners(:ashley) + @pet1, @pet2 = pets(:chew), pets(:mochi) + + @params = { + :pets_attributes => { + '0' => { :id => @pet1.id, :name => 'Foo' }, + '1' => { :id => @pet2.id, :name => 'Bar' } + } + } + end + + def test_should_update_existing_records_with_non_standard_primary_key + @owner.update_attributes(@params) + assert_equal ['Foo', 'Bar'], @owner.pets.map(&:name) + end +end |