Skip to content

Commit b2ced2f

Browse files
committed
registrations are unique for exam,user pairs
1 parent 125ec79 commit b2ced2f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

app/models/registration.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ class Registration < ApplicationRecord
2828

2929
validates :user, uniqueness: { scope: :exam_version }
3030

31+
validate :user_exam_uniqueness
32+
def user_exam_uniqueness
33+
other_reg_exists =
34+
user
35+
.registrations
36+
.where(exam_version: exam.exam_versions)
37+
.where.not(exam_version: exam_version)
38+
.any?
39+
errors.add(:user, 'already has a registration for another version of that exam') if other_reg_exists
40+
end
41+
3142
def room_version_same_exam
3243
return unless room
3344

test/models/user_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ class UserTest < ActiveSupport::TestCase
1818
assert_match(/has already been taken/, r2.errors.full_messages.to_sentence)
1919
assert_not r2.save
2020
end
21+
22+
test 'cannot create two registrations for the same Exam-User pair' do
23+
r1 = create(:registration)
24+
second_version = create(:exam_version, exam: r1.exam)
25+
r2 = build(:registration, user: r1.user, exam_version: second_version)
26+
assert_not r2.valid?
27+
assert_match(/has a registration for another version/, r2.errors.full_messages.to_sentence)
28+
assert_not r2.save
29+
end
2130
end

0 commit comments

Comments
 (0)