Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow primary_key: association option to be composite #51345

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/reflection.rb
Expand Up @@ -870,7 +870,11 @@ def association_class
# klass option is necessary to support loading polymorphic associations
def association_primary_key(klass = nil)
if primary_key = options[:primary_key]
@association_primary_key ||= -primary_key.to_s
@association_primary_key ||= if primary_key.is_a?(Array)
primary_key.map { |pk| pk.to_s.freeze }.freeze
else
-primary_key.to_s
end
elsif (klass || self.klass).has_query_constraints? || options[:query_constraints]
(klass || self.klass).composite_query_constraints_list
elsif (klass || self.klass).composite_primary_key?
Expand Down
Expand Up @@ -371,6 +371,18 @@ def test_building_the_belonging_object_for_composite_primary_key
assert_equal id, cpk_book.order_id
end

def test_belongs_to_with_explicit_composite_primary_key
cpk_book = cpk_books(:cpk_great_author_first_book)
order = cpk_book.build_order_explicit_fk_pk
order.shop_id = 123
cpk_book.save

shop_id, id = order.id
assert_equal id, cpk_book.order_id
assert_equal shop_id, cpk_book.shop_id
assert_equal order, cpk_book.reload.order_explicit_fk_pk
end

def test_belongs_to_with_inverse_association_for_composite_primary_key
author = Cpk::Author.new(name: "John")
book = author.books.build(id: [nil, 1], title: "The Rails Way")
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/cpk/book.rb
Expand Up @@ -6,6 +6,7 @@ class Book < ActiveRecord::Base

self.table_name = :cpk_books
belongs_to :order, autosave: true, query_constraints: [:shop_id, :order_id], counter_cache: true
belongs_to :order_explicit_fk_pk, class_name: "Cpk::Order", query_constraints: [:shop_id, :order_id], primary_key: [:shop_id, :id]
belongs_to :author, class_name: "Cpk::Author"

has_many :chapters, query_constraints: [:author_id, :book_id]
Expand Down