Skip to content

Commit 2c2dde3

Browse files
committed
Add failing test case for preloading polymorphic association across multiple databases with same table name
1 parent 860ab28 commit 2c2dde3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

activerecord/test/cases/associations_test.rb

Lines changed: 21 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,23 @@ 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+
assert_queries(2) do
1224+
preloader = ActiveRecord::Associations::Preloader.new(records: [dog_comment, other_dog_comment], associations: :origin)
1225+
preloader.call
1226+
end
1227+
end
1228+
12091229
def test_preload_with_available_records
12101230
post = posts(:welcome)
12111231
david = authors(:david)

0 commit comments

Comments
 (0)