File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
lib/active_record/attribute_methods Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,11 @@ def id
23
23
24
24
# Sets the primary key column's value.
25
25
def id = ( value )
26
- _write_attribute ( @primary_key , value )
26
+ if self . class . composite_primary_key?
27
+ @primary_key . zip ( value ) { |attr , value | _write_attribute ( attr , value ) }
28
+ else
29
+ _write_attribute ( @primary_key , value )
30
+ end
27
31
end
28
32
29
33
# Queries the primary key column's value.
Original file line number Diff line number Diff line change @@ -373,6 +373,22 @@ def test_composite_primary_key_out_of_order
373
373
assert_equal [ "code" , "region" ] , @connection . primary_keys ( "barcodes_reverse" )
374
374
end
375
375
376
+ def test_assigning_a_composite_primary_key
377
+ book = Cpk ::Book . new
378
+ book . id = [ 1 , 2 ]
379
+ book . save!
380
+
381
+ assert_equal [ 1 , 2 ] , book . id
382
+ end
383
+
384
+ def test_assigning_a_non_array_value_to_model_with_composite_primary_key_raises
385
+ book = Cpk ::Book . new
386
+
387
+ assert_raises ( TypeError ) do
388
+ book . id = 1
389
+ end
390
+ end
391
+
376
392
def test_primary_key_issues_warning
377
393
model = Class . new ( ActiveRecord ::Base ) do
378
394
def self . table_name
You can’t perform that action at this time.
0 commit comments