Skip to content

Commit 807bd54

Browse files
authored
Merge pull request rails#48650 from gmcgibbon/cpk_hmt_singular
Make has_many through singular associations build CPK records
2 parents d1a79da + 3fc9ade commit 807bd54

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

activerecord/lib/active_record/associations/through_association.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ def build_record(attributes)
118118
target = through_association.target
119119

120120
if inverse && target && !target.is_a?(Array)
121-
attributes[inverse.foreign_key] = target.id
121+
Array(target.id).zip(Array(inverse.foreign_key)).map do |primary_key_value, foreign_key_column|
122+
attributes[foreign_key_column] = primary_key_value
123+
end
122124
end
123125

124126
super

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,14 @@ def test_cpk_stale_target
16501650
assert_predicate(book.association(:order_agreements), :stale_target?)
16511651
end
16521652

1653+
def test_cpk_association_build_through_singular
1654+
order = Cpk::OrderWithSingularBookChapters.create!(id: [1, 2])
1655+
book = order.create_book!(id: [3, 4])
1656+
chapter = order.chapters.build
1657+
1658+
assert_equal(chapter.book, book)
1659+
end
1660+
16531661
private
16541662
def make_model(name)
16551663
Class.new(ActiveRecord::Base) { define_singleton_method(:name) { name } }

activerecord/test/models/cpk/book.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Book < ActiveRecord::Base
77
belongs_to :order, autosave: true, query_constraints: [:shop_id, :order_id]
88
belongs_to :author, class_name: "Cpk::Author"
99

10-
has_many :chapters, query_constraints: [:author_id, :book_number]
10+
has_many :chapters, query_constraints: [:author_id, :book_id]
1111
end
1212

1313
class BestSeller < Book
@@ -18,7 +18,7 @@ class BrokenBook < Book
1818
end
1919

2020
class NullifiedBook < Book
21-
has_one :chapter, query_constraints: [:author_id, :book_number], dependent: :nullify
21+
has_one :chapter, query_constraints: [:author_id, :book_id], dependent: :nullify
2222
end
2323

2424
class BookWithOrderAgreements < Book

activerecord/test/models/cpk/chapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class Chapter < ActiveRecord::Base
77
# to be shared between different databases
88
self.primary_key = [:author_id, :id]
99

10-
belongs_to :book, query_constraints: [:author_id, :book_number]
10+
belongs_to :book, query_constraints: [:author_id, :book_id]
1111
end
1212
end

activerecord/test/models/cpk/order.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ class OrderWithPrimaryKeyAssociatedBook < Order
2424
class OrderWithNullifiedBook < Order
2525
has_one :book, query_constraints: [:shop_id, :order_id], dependent: :nullify
2626
end
27+
28+
class OrderWithSingularBookChapters < Order
29+
has_many :chapters, through: :book
30+
end
2731
end

0 commit comments

Comments
 (0)