From f194d65f36be29971601664c880cb9b23ee2a95b Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 4 May 2010 22:42:25 +0100 Subject: [PATCH] Use primary key in conditions, not 'id' [#4395 state:resolved] Signed-off-by: Pratik Naik Conflicts: activerecord/test/cases/nested_attributes_test.rb --- .../lib/active_record/nested_attributes.rb | 2 +- .../test/cases/nested_attributes_test.rb | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 92882390fccbb..65434fb06b645 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -346,7 +346,7 @@ def assign_nested_attributes_for_collection_association(association_name, attrib 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 9f8534ce6d19e..c001db6220f1c 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -6,6 +6,8 @@ require "models/treasure" require "models/man" require "models/interest" +require "models/owner" +require "models/pet" module AssertRaiseWithMessage def assert_raise_with_message(expected_exception, expected_message) @@ -706,3 +708,26 @@ def test_limit_with_exceeding_records 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