File tree Expand file tree Collapse file tree 3 files changed +17
-2
lines changed
lib/active_record/relation/predicate_builder Expand file tree Collapse file tree 3 files changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,11 @@ def initialize(associated_table, value)
9
9
end
10
10
11
11
def queries
12
- [ associated_table . join_foreign_key => ids ]
12
+ if associated_table . join_foreign_key . is_a? ( Array )
13
+ ids . map { |ids_set | associated_table . join_foreign_key . zip ( ids_set ) . to_h }
14
+ else
15
+ [ associated_table . join_foreign_key => ids ]
16
+ end
13
17
end
14
18
15
19
private
@@ -31,6 +35,8 @@ def primary_key
31
35
end
32
36
33
37
def convert_to_id ( value )
38
+ return primary_key . map { |pk | value . public_send ( pk ) } if primary_key . is_a? ( Array )
39
+
34
40
if value . respond_to? ( primary_key )
35
41
value . public_send ( primary_key )
36
42
else
Original file line number Diff line number Diff line change @@ -147,6 +147,15 @@ def test_belongs_to_a_model_with_composite_primary_key_uses_composite_pk_in_sql
147
147
assert_match ( /#{ Regexp . escape ( Sharded ::BlogPost . connection . quote_table_name ( "sharded_blog_posts.id" ) ) } =/ , sql )
148
148
end
149
149
150
+ def test_querying_by_whole_associated_records_using_query_constraints
151
+ comments = [ sharded_comments ( :great_comment_blog_post_one ) , sharded_comments ( :great_comment_blog_post_two ) ]
152
+
153
+ blog_posts = Sharded ::BlogPost . where ( comments : comments ) . to_a
154
+
155
+ expected_posts = [ sharded_blog_posts ( :great_post_blog_one ) , sharded_blog_posts ( :great_post_blog_two ) ]
156
+ assert_equal ( expected_posts . map ( &:id ) . sort , blog_posts . map ( &:id ) . sort )
157
+ end
158
+
150
159
def test_has_many_association_with_composite_foreign_key_loads_records
151
160
blog_post = sharded_blog_posts ( :great_post_blog_one )
152
161
Original file line number Diff line number Diff line change @@ -18,5 +18,5 @@ unique_comment_blog_post_one:
18
18
19
19
great_comment_blog_post_two :
20
20
body : " I really enjoyed the post!"
21
- blog_post_id : <%= ActiveRecord::FixtureSet.identify(:great_blog_post_two ) %>
21
+ blog_post_id : <%= ActiveRecord::FixtureSet.identify(:great_post_blog_two ) %>
22
22
blog_id : <%= ActiveRecord::FixtureSet.identify(:sharded_blog_two) %>
You can’t perform that action at this time.
0 commit comments