Skip to content

Commit

Permalink
Merge pull request #48650 from gmcgibbon/cpk_hmt_singular
Browse files Browse the repository at this point in the history
Make has_many through singular associations build CPK records
  • Loading branch information
eileencodes committed Jul 5, 2023
2 parents d1a79da + 3fc9ade commit 807bd54
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def build_record(attributes)
target = through_association.target

if inverse && target && !target.is_a?(Array)
attributes[inverse.foreign_key] = target.id
Array(target.id).zip(Array(inverse.foreign_key)).map do |primary_key_value, foreign_key_column|
attributes[foreign_key_column] = primary_key_value
end
end

super
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,14 @@ def test_cpk_stale_target
assert_predicate(book.association(:order_agreements), :stale_target?)
end

def test_cpk_association_build_through_singular
order = Cpk::OrderWithSingularBookChapters.create!(id: [1, 2])
book = order.create_book!(id: [3, 4])
chapter = order.chapters.build

assert_equal(chapter.book, book)
end

private
def make_model(name)
Class.new(ActiveRecord::Base) { define_singleton_method(:name) { name } }
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/models/cpk/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Book < ActiveRecord::Base
belongs_to :order, autosave: true, query_constraints: [:shop_id, :order_id]
belongs_to :author, class_name: "Cpk::Author"

has_many :chapters, query_constraints: [:author_id, :book_number]
has_many :chapters, query_constraints: [:author_id, :book_id]
end

class BestSeller < Book
Expand All @@ -18,7 +18,7 @@ class BrokenBook < Book
end

class NullifiedBook < Book
has_one :chapter, query_constraints: [:author_id, :book_number], dependent: :nullify
has_one :chapter, query_constraints: [:author_id, :book_id], dependent: :nullify
end

class BookWithOrderAgreements < Book
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/cpk/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class Chapter < ActiveRecord::Base
# to be shared between different databases
self.primary_key = [:author_id, :id]

belongs_to :book, query_constraints: [:author_id, :book_number]
belongs_to :book, query_constraints: [:author_id, :book_id]
end
end
4 changes: 4 additions & 0 deletions activerecord/test/models/cpk/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ class OrderWithPrimaryKeyAssociatedBook < Order
class OrderWithNullifiedBook < Order
has_one :book, query_constraints: [:shop_id, :order_id], dependent: :nullify
end

class OrderWithSingularBookChapters < Order
has_many :chapters, through: :book
end
end

0 comments on commit 807bd54

Please sign in to comment.