Skip to content

Commit c5f3a81

Browse files
authored
Merge pull request rails#50382 from summera/polymorphic-multi-db-preload-fix
Fix multi-database polymorphic preloading with equivalent table names
2 parents c6ef898 + def54b2 commit c5f3a81

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

activerecord/lib/active_record/associations/preloader/association.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ def initialize(scope, association_key_name)
1717
def eql?(other)
1818
association_key_name == other.association_key_name &&
1919
scope.table_name == other.scope.table_name &&
20+
scope.connection_specification_name == other.scope.connection_specification_name &&
2021
scope.values_for_queries == other.scope.values_for_queries
2122
end
2223

2324
def hash
24-
[association_key_name, scope.table_name, scope.values_for_queries].hash
25+
[association_key_name, scope.table_name, scope.connection_specification_name, scope.values_for_queries].hash
2526
end
2627

2728
def records_for(loaders)

activerecord/test/cases/associations_test.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
require "models/cpk"
4040
require "models/member_detail"
4141
require "models/organization"
42+
require "models/dog"
43+
require "models/other_dog"
4244

4345

4446
class AssociationsTest < ActiveRecord::TestCase
@@ -773,7 +775,8 @@ def test_associations_raise_with_name_error_if_associated_to_classes_that_do_not
773775
class PreloaderTest < ActiveRecord::TestCase
774776
fixtures :posts, :comments, :books, :authors, :tags, :taggings, :essays, :categories, :author_addresses,
775777
:sharded_blog_posts, :sharded_comments, :sharded_blog_posts_tags, :sharded_tags,
776-
:members, :member_details, :organizations, :cpk_orders, :cpk_order_agreements
778+
:members, :member_details, :organizations, :cpk_orders, :cpk_order_agreements,
779+
:dogs, :other_dogs
777780

778781
def test_preload_with_scope
779782
post = posts(:welcome)
@@ -1206,6 +1209,31 @@ def test_preload_does_not_group_same_scope_different_key_name
12061209
end
12071210
end
12081211

1212+
def test_multi_database_polymorphic_preload_with_same_table_name
1213+
dog = dogs(:sophie)
1214+
dog_comment = comments(:greetings)
1215+
dog_comment.origin_type = dog.class.name
1216+
dog_comment.origin_id = dog.id
1217+
1218+
other_dog = other_dogs(:lassie)
1219+
other_dog_comment = comments(:more_greetings)
1220+
other_dog_comment.origin_type = other_dog.class.name
1221+
other_dog_comment.origin_id = other_dog.id
1222+
1223+
# Both Dog and OtherDog are backed by a table named `dogs`,
1224+
# however they are stored in different databases and should
1225+
# therefore result in two separate queries rather than be batched
1226+
# together.
1227+
#
1228+
# Expected
1229+
# SELECT FROM dogs ... (Dog)
1230+
# SELECT FROM dogs ... (OtherDog)
1231+
assert_queries(2) do
1232+
preloader = ActiveRecord::Associations::Preloader.new(records: [dog_comment, other_dog_comment], associations: :origin)
1233+
preloader.call
1234+
end
1235+
end
1236+
12091237
def test_preload_with_available_records
12101238
post = posts(:welcome)
12111239
david = authors(:david)

0 commit comments

Comments
 (0)