File tree Expand file tree Collapse file tree 2 files changed +16
-21
lines changed
lib/active_record/connection_adapters/sqlserver/core_ext Expand file tree Collapse file tree 2 files changed +16
-21
lines changed Original file line number Diff line number Diff line change @@ -8,10 +8,22 @@ module SQLServer
88 module CoreExt
99 module LoaderQuery
1010 def load_records_for_keys ( keys , &block )
11- return super unless scope . connection . adapter_name == "SQLServer"
11+ return super unless scope . connection . sqlserver?
1212
13- keys . each_slice ( in_clause_length ) . flat_map do |slice |
14- scope . where ( association_key_name => slice ) . load ( &block ) . records
13+ if association_key_name . is_a? ( Array )
14+ query_constraints = Hash . new { |hsh , key | hsh [ key ] = Set . new }
15+
16+ keys . each_with_object ( query_constraints ) do |values_set , constraints |
17+ association_key_name . zip ( values_set ) . each do |key_name , value |
18+ constraints [ key_name ] << value
19+ end
20+ end
21+
22+ scope . where ( query_constraints ) . load ( &block )
23+ else
24+ keys . each_slice ( in_clause_length ) . flat_map do |slice |
25+ scope . where ( association_key_name => slice ) . load ( &block ) . records
26+ end
1527 end
1628 end
1729
Original file line number Diff line number Diff line change @@ -956,25 +956,8 @@ def test_count_with_include_coerced
956956 assert_equal 3 , authors ( :david ) . posts_with_comments . where ( "LEN(comments.body) > 15" ) . references ( :comments ) . count
957957 end
958958
959- # Use TOP (1) in scope vs limit 1.
959+ # The raw SQL in the scope uses ` limit 1` .
960960 coerce_tests! %r{including association based on sql condition and no database column}
961-
962- # Table name not regex escaped in original test. Remove coerced test when https://github.com/rails/rails/pull/49345 merged.
963- coerce_tests! %r{preloading belongs_to association SQL}
964- test "preloading belongs_to association SQL coerced" do
965- blog_ids = [ sharded_blogs ( :sharded_blog_one ) . id , sharded_blogs ( :sharded_blog_two ) . id ]
966- posts = Sharded ::BlogPost . where ( blog_id : blog_ids ) . includes ( :comments )
967-
968- sql = capture_sql do
969- comments_collection = posts . map ( &:comments )
970- assert_equal 3 , comments_collection . size
971- end . last
972-
973- c = Sharded ::BlogPost . connection
974- quoted_blog_id = Regexp . escape ( c . quote_table_name ( "sharded_comments.blog_id" ) )
975- quoted_blog_post_id = Regexp . escape ( c . quote_table_name ( "sharded_comments.blog_post_id" ) )
976- assert_match ( /WHERE #{ quoted_blog_id } IN \( .+\) AND #{ quoted_blog_post_id } IN \( .+\) / , sql )
977- end
978961end
979962
980963require "models/topic"
You can’t perform that action at this time.
0 commit comments