@@ -226,6 +226,17 @@ def test_has_many_reflection
226
226
assert_equal "companies" , Firm . reflect_on_association ( :clients_of_firm ) . table_name
227
227
end
228
228
229
+ def test_has_many_reflection_with_array_fk_raises
230
+ expected_message = <<~MSG . squish
231
+ Passing [:firm_id, :firm_name] array to :foreign_key option
232
+ on the Firm#clients association is not supported.
233
+ Use the query_constraints: [:firm_id, :firm_name] option instead to represent a composite foreign key.
234
+ MSG
235
+ assert_raises ArgumentError , match : expected_message do
236
+ ActiveRecord ::Reflection . create ( :has_many , :clients , nil , { foreign_key : [ :firm_id , :firm_name ] } , Firm )
237
+ end
238
+ end
239
+
229
240
def test_has_one_reflection
230
241
reflection_for_account = ActiveRecord ::Reflection . create ( :has_one , :account , nil , { foreign_key : "firm_id" , dependent : :destroy } , Firm )
231
242
assert_equal reflection_for_account , Firm . reflect_on_association ( :account )
@@ -234,6 +245,17 @@ def test_has_one_reflection
234
245
assert_equal "accounts" , Firm . reflect_on_association ( :account ) . table_name
235
246
end
236
247
248
+ def has_one_reflection_with_array_fk_raises
249
+ expected_message = <<~MSG . squish
250
+ Passing [:firm_id, :firm_name] array to :foreign_key option
251
+ on the Firm#account association is not supported.
252
+ Use the query_constraints: [:firm_id, :firm_name] option instead to represent a composite foreign key.
253
+ MSG
254
+ assert_raises ArgumentError , match : expected_message do
255
+ ActiveRecord ::Reflection . create ( :has_one , :account , nil , { foreign_key : [ :firm_id , :firm_name ] } , Firm )
256
+ end
257
+ end
258
+
237
259
def test_belongs_to_inferred_foreign_key_from_assoc_name
238
260
Company . belongs_to :foo
239
261
assert_equal "foo_id" , Company . reflect_on_association ( :foo ) . foreign_key
@@ -243,6 +265,17 @@ def test_belongs_to_inferred_foreign_key_from_assoc_name
243
265
assert_equal "xyzzy_id" , Company . reflect_on_association ( :baz ) . foreign_key
244
266
end
245
267
268
+ def test_belongs_to_reflection_with_array_fk_raises
269
+ expected_message = <<~MSG . squish
270
+ Passing [:firm_id, :firm_name] array to :foreign_key option
271
+ on the Firm#client association is not supported.
272
+ Use the query_constraints: [:firm_id, :firm_name] option instead to represent a composite foreign key.
273
+ MSG
274
+ assert_raises ArgumentError , match : expected_message do
275
+ ActiveRecord ::Reflection . create ( :belongs_to , :client , nil , { foreign_key : [ :firm_id , :firm_name ] } , Firm )
276
+ end
277
+ end
278
+
246
279
def test_association_reflection_in_modules
247
280
ActiveRecord ::Base . store_full_sti_class = false
248
281
0 commit comments