File tree Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Expand file tree Collapse file tree 2 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -416,8 +416,16 @@ def restore_transaction_record_state(force_restore_state = false)
416
416
end
417
417
@mutations_from_database = nil
418
418
@mutations_before_last_save = nil
419
- if @attributes . fetch_value ( @primary_key ) != restore_state [ :id ]
420
- @attributes . write_from_user ( @primary_key , restore_state [ :id ] )
419
+ if self . class . composite_primary_key?
420
+ if restore_state [ :id ] != @primary_key . map { |col | @attributes . fetch_value ( col ) }
421
+ @primary_key . zip ( restore_state [ :id ] ) . each do |col , val |
422
+ @attributes . write_from_user ( col , val )
423
+ end
424
+ end
425
+ else
426
+ if @attributes . fetch_value ( @primary_key ) != restore_state [ :id ]
427
+ @attributes . write_from_user ( @primary_key , restore_state [ :id ] )
428
+ end
421
429
end
422
430
freeze if restore_state [ :frozen? ]
423
431
end
Original file line number Diff line number Diff line change 9
9
require "models/author"
10
10
require "models/post"
11
11
require "models/movie"
12
+ require "models/cpk"
12
13
13
14
class TransactionTest < ActiveRecord ::TestCase
14
15
self . use_transactional_tests = false
@@ -926,6 +927,32 @@ def test_restore_previously_new_record_after_double_save
926
927
assert_predicate topic , :previously_new_record?
927
928
end
928
929
930
+ def test_restore_composite_id_after_rollback
931
+ book = Cpk ::Book . create! ( author_id : 1 , number : 2 )
932
+
933
+ Cpk ::Book . transaction do
934
+ book . update! ( author_id : 42 , number : 42 )
935
+ raise ActiveRecord ::Rollback
936
+ end
937
+
938
+ assert_equal [ 1 , 2 ] , book . id
939
+ ensure
940
+ Cpk ::Book . delete_all
941
+ end
942
+
943
+ def test_rollback_on_composite_key_model
944
+ Cpk ::Book . create! ( author_id : 1 , number : 3 , title : "Charlotte's Web" )
945
+ book_two_unpersisted = Cpk ::Book . new ( author_id : 1 , number : 3 )
946
+
947
+ assert_raise ( ActiveRecord ::RecordNotUnique ) do
948
+ Cpk ::Book . transaction do
949
+ book_two_unpersisted . save!
950
+ end
951
+ end
952
+ ensure
953
+ Cpk ::Book . delete_all
954
+ end
955
+
929
956
def test_restore_id_after_rollback
930
957
topic = Topic . new
931
958
You can’t perform that action at this time.
0 commit comments