|
39 | 39 | require "models/cpk"
|
40 | 40 | require "models/member_detail"
|
41 | 41 | require "models/organization"
|
| 42 | +require "models/dog" |
| 43 | +require "models/other_dog" |
42 | 44 |
|
43 | 45 |
|
44 | 46 | class AssociationsTest < ActiveRecord::TestCase
|
@@ -773,7 +775,8 @@ def test_associations_raise_with_name_error_if_associated_to_classes_that_do_not
|
773 | 775 | class PreloaderTest < ActiveRecord::TestCase
|
774 | 776 | fixtures :posts, :comments, :books, :authors, :tags, :taggings, :essays, :categories, :author_addresses,
|
775 | 777 | :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 |
777 | 780 |
|
778 | 781 | def test_preload_with_scope
|
779 | 782 | post = posts(:welcome)
|
@@ -1206,6 +1209,31 @@ def test_preload_does_not_group_same_scope_different_key_name
|
1206 | 1209 | end
|
1207 | 1210 | end
|
1208 | 1211 |
|
| 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 | + |
1209 | 1237 | def test_preload_with_available_records
|
1210 | 1238 | post = posts(:welcome)
|
1211 | 1239 | david = authors(:david)
|
|
0 commit comments