Skip to content

Commit 3859037

Browse files
authored
Merge pull request rails#47925 from eileencodes/ensure-ids_writer-and-ids_reader-accomodate-cpk
Ensure that ids_writer and ids_reader is working for CPK
2 parents 12fc98f + 885bd85 commit 3859037

File tree

8 files changed

+61
-5
lines changed

8 files changed

+61
-5
lines changed

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ def ids_writer(ids)
6363
ids = Array(ids).compact_blank
6464
ids.map! { |i| pk_type.cast(i) }
6565

66-
records = klass.where(primary_key => ids).index_by do |r|
67-
r.public_send(primary_key)
66+
records = if klass.composite_primary_key?
67+
query_records = ids.map { |values_set| klass.where(primary_key.zip(values_set).to_h) }.inject(&:or)
68+
69+
query_records.index_by do |r|
70+
primary_key.map { |pk| r.public_send(pk) }
71+
end
72+
else
73+
klass.where(primary_key => ids).index_by do |r|
74+
r.public_send(primary_key)
75+
end
6876
end.values_at(*ids).compact
6977

7078
if records.size != ids.size

activerecord/lib/active_record/relation/calculations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def pluck(*column_names)
269269
relation = apply_join_dependency
270270
relation.pluck(*column_names)
271271
else
272-
klass.disallow_raw_sql!(column_names)
272+
klass.disallow_raw_sql!(column_names.flatten)
273273
columns = arel_columns(column_names)
274274
relation = spawn
275275
relation.select_values = columns

activerecord/test/cases/autosave_association_test.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
require "models/chef"
4040
require "models/cake_designer"
4141
require "models/drink_designer"
42+
require "models/cpk"
4243

4344
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
4445
def test_autosave_works_even_when_other_callbacks_update_the_parent_model
@@ -596,7 +597,7 @@ def test_valid_adding_with_nested_attributes
596597
end
597598

598599
class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
599-
fixtures :companies, :developers
600+
fixtures :companies, :developers, :cpk_order_agreements, :cpk_orders, :cpk_books
600601

601602
def test_invalid_adding
602603
firm = Firm.find(1)
@@ -730,6 +731,37 @@ def test_assign_ids
730731
assert_includes firm.clients, companies(:second_client)
731732
end
732733

734+
def test_assign_ids_with_belongs_to_cpk_model
735+
order_agreements = [cpk_order_agreements(:order_agreement_one).id, cpk_order_agreements(:order_agreement_two).id]
736+
order = cpk_orders(:cpk_groceries_order_1)
737+
738+
assert_empty order.order_agreements
739+
740+
order.order_agreement_ids = order_agreements
741+
order.save
742+
order.reload
743+
744+
assert_equal order_agreements, order.order_agreement_ids
745+
assert_equal 2, order.order_agreements.length
746+
assert_includes order.order_agreements, cpk_order_agreements(:order_agreement_two)
747+
end
748+
749+
def test_assign_ids_with_cpk_for_two_models
750+
books = [cpk_books(:cpk_great_author_first_book).id, cpk_books(:cpk_great_author_second_book).id]
751+
order = cpk_orders(:cpk_groceries_order_1)
752+
753+
assert_empty order.books
754+
755+
order.book_ids = books
756+
order.save
757+
order.reload
758+
759+
assert_equal books, order.book_ids
760+
assert_equal 2, order.books.length
761+
assert_includes order.books, cpk_books(:cpk_great_author_first_book)
762+
assert_includes order.books, cpk_books(:cpk_great_author_second_book)
763+
end
764+
733765
def test_assign_ids_for_through_a_belongs_to
734766
firm = Firm.new("name" => "Apple")
735767
firm.developer_ids = [developers(:david).id, developers(:jamis).id]

activerecord/test/cases/calculations_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,14 @@ def test_ids
941941
assert_equal Company.all.map(&:id).sort, Company.all.ids.sort
942942
end
943943

944-
def ids_for_a_composite_primary_key
944+
def test_ids_for_a_composite_primary_key
945945
assert_equal Cpk::Book.all.map(&:id).sort, Cpk::Book.all.ids.sort
946946
end
947947

948+
def test_pluck_for_a_composite_primary_key
949+
assert_equal Cpk::Book.all.pluck([:author_id, :number]).sort, Cpk::Book.all.ids.sort
950+
end
951+
948952
def test_ids_for_a_composite_primary_key_with_scope
949953
book = cpk_books(:cpk_great_author_first_book)
950954

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
_fixture:
2+
model_class: Cpk::OrderAgreement
3+
4+
order_agreement_one:
5+
signature: "abc123"
6+
7+
order_agreement_two:
8+
signature: "xyz789"

activerecord/test/models/cpk/book.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Cpk
44
class Book < ActiveRecord::Base
55
self.table_name = :cpk_books
66
self.primary_key = [:author_id, :number]
7+
8+
belongs_to :order
79
end
810

911
class BestSeller < Book

activerecord/test/models/cpk/order.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ class Order < ActiveRecord::Base
66
self.primary_key = [:shop_id, :id]
77

88
has_many :order_agreements, primary_key: :id
9+
has_many :books
910
end
1011
end

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
t.integer :number
245245
t.string :title
246246
t.integer :revision
247+
t.integer :order_id
247248
end
248249

249250
create_table :cpk_orders, primary_key: [:shop_id, :id], force: true do |t|

0 commit comments

Comments
 (0)