Skip to content

Commit

Permalink
Use primary key in conditions, not 'id' [#4395 state:resolved]
Browse files Browse the repository at this point in the history
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>

Conflicts:

	activerecord/test/cases/nested_attributes_test.rb
  • Loading branch information
lifo committed May 4, 2010
1 parent aeff171 commit f194d65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/nested_attributes.rb
Expand Up @@ -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|
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/nested_attributes_test.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit f194d65

Please sign in to comment.