Skip to content
Browse files

Use primary key in conditions, not 'id' [#4395 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent bce2c0c commit 38da0ace772e6f9f5e2fff74db76237ab31790fa @mat813 mat813 committed with lifo
View
2 activerecord/lib/active_record/nested_attributes.rb
@@ -355,7 +355,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|
View
25 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"
require 'active_support/hash_with_indifferent_access'
module AssertRaiseWithMessage
@@ -707,3 +709,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

0 comments on commit 38da0ac

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