Skip to content

Commit e684588

Browse files
Fix #scope_for_create for belongs_to associations with CPK
`#scope_for_create` for singular associations removes the primary key from the scope so that we don't assign PK columns when building an association. However, removing the primary key from the scope doesn't currently handle composite primary keys. This commit fixes that.
1 parent 30aee98 commit e684588

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

activerecord/lib/active_record/associations/singular_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def force_reload_reader
3434

3535
private
3636
def scope_for_create
37-
super.except!(klass.primary_key)
37+
super.except!(*Array(klass.primary_key))
3838
end
3939

4040
def find_target

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
class BelongsToAssociationsTest < ActiveRecord::TestCase
3636
fixtures :accounts, :companies, :developers, :projects, :topics,
3737
:developers_projects, :computers, :authors, :author_addresses,
38-
:essays, :posts, :tags, :taggings, :comments, :sponsors, :members, :nodes
38+
:essays, :posts, :tags, :taggings, :comments, :sponsors, :members, :nodes, :cpk_books
3939

4040
def test_belongs_to
4141
client = Client.find(3)
@@ -356,6 +356,15 @@ def test_building_the_belonging_object
356356
assert_equal apple.id, citibank.firm_id
357357
end
358358

359+
def test_building_the_belonging_object_for_composite_primary_key
360+
cpk_book = cpk_books(:cpk_great_author_first_book)
361+
order = cpk_book.build_order
362+
cpk_book.save
363+
364+
_shop_id, id = order.id
365+
assert_equal id, cpk_book.order_id
366+
end
367+
359368
def test_building_the_belonging_object_with_implicit_sti_base_class
360369
account = Account.new
361370
company = account.build_firm

0 commit comments

Comments
 (0)