From 400d122e52c7136ddc75add13af68aa827fcdcf4 Mon Sep 17 00:00:00 2001 From: Jamie Benstead Date: Fri, 20 Mar 2026 14:13:56 +0000 Subject: [PATCH 1/6] Change creator_id to only need to be unique for schools where rejected_at is nil --- app/models/school.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/school.rb b/app/models/school.rb index 2f14500b0..cea891f58 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -31,7 +31,11 @@ class School < ApplicationRecord format: { with: /\A[0-9]+[A-Z]+\z/, allow_nil: true, message: I18n.t('validations.school.school_roll_number') }, presence: true, if: :ireland? - validates :creator_id, presence: true, uniqueness: true + validates :creator_id, + presence: true, + uniqueness: { + conditions: -> { where(rejected_at: nil) } + } validates :creator_agree_authority, presence: true, acceptance: true validates :creator_agree_terms_and_conditions, presence: true, acceptance: true validates :creator_agree_responsible_safeguarding, presence: true, acceptance: true From aa910520119e591f91e9598fc1859346e730f1ed Mon Sep 17 00:00:00 2001 From: Jamie Benstead Date: Mon, 23 Mar 2026 14:48:23 +0000 Subject: [PATCH 2/6] Update find_for_user to only return user where rejected_at is nil --- app/models/school.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/school.rb b/app/models/school.rb index cea891f58..0080abeeb 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -57,7 +57,7 @@ class School < ApplicationRecord after_create :generate_code!, if: -> { FeatureFlags.immediate_school_onboarding? } def self.find_for_user!(user) - school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id) + school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id, rejected_at: nil) raise ActiveRecord::RecordNotFound unless school school From c84d839fc4ba714aa99f90f52b3c75ca7e0ef92f Mon Sep 17 00:00:00 2001 From: Jamie Benstead Date: Mon, 23 Mar 2026 15:25:51 +0000 Subject: [PATCH 3/6] Add migration so creator_id is only unique on table where rejected_at is null --- ...260323152328_update_creator_id_index_on_schools.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 db/migrate/20260323152328_update_creator_id_index_on_schools.rb diff --git a/db/migrate/20260323152328_update_creator_id_index_on_schools.rb b/db/migrate/20260323152328_update_creator_id_index_on_schools.rb new file mode 100644 index 000000000..1ab2e128e --- /dev/null +++ b/db/migrate/20260323152328_update_creator_id_index_on_schools.rb @@ -0,0 +1,11 @@ +class UpdateCreatorIdIndexOnSchools < ActiveRecord::Migration[7.2] + def change + remove_index :schools, name: "index_schools_on_creator_id" + + add_index :schools, + :creator_id, + unique: true, + where: "rejected_at IS NULL", + name: "index_schools_on_creator_id_active_only" + end +end From d5b80e74fec4e661898adcc89464c0a086b6cac6 Mon Sep 17 00:00:00 2001 From: Jamie Benstead Date: Mon, 23 Mar 2026 15:36:37 +0000 Subject: [PATCH 4/6] Change migration to be reversible if needed --- ...0323152328_update_creator_id_index_on_schools.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/db/migrate/20260323152328_update_creator_id_index_on_schools.rb b/db/migrate/20260323152328_update_creator_id_index_on_schools.rb index 1ab2e128e..f7698eb83 100644 --- a/db/migrate/20260323152328_update_creator_id_index_on_schools.rb +++ b/db/migrate/20260323152328_update_creator_id_index_on_schools.rb @@ -1,5 +1,5 @@ class UpdateCreatorIdIndexOnSchools < ActiveRecord::Migration[7.2] - def change + def up remove_index :schools, name: "index_schools_on_creator_id" add_index :schools, @@ -8,4 +8,13 @@ def change where: "rejected_at IS NULL", name: "index_schools_on_creator_id_active_only" end -end + + def down + remove_index :schools, name: "index_schools_on_creator_id_active_only" + + add_index :schools, + :creator_id, + unique: true, + name: "index_schools_on_creator_id" + end +end \ No newline at end of file From 9c5b01c8857e323866f48eb5fadb732c16d356e3 Mon Sep 17 00:00:00 2001 From: Jamie Benstead Date: Tue, 24 Mar 2026 09:26:57 +0000 Subject: [PATCH 5/6] Run db migration --- db/schema.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index dc022dbea..1699e359f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_03_19_084433) do +ActiveRecord::Schema[7.2].define(version: 2026_03_23_152328) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -328,7 +328,7 @@ t.string "district_nces_id" t.string "school_roll_number" t.index ["code"], name: "index_schools_on_code", unique: true - t.index ["creator_id"], name: "index_schools_on_creator_id", unique: true + t.index ["creator_id"], name: "index_schools_on_creator_id_active_only", unique: true, where: "(rejected_at IS NULL)" t.index ["reference"], name: "index_schools_on_reference", unique: true, where: "(rejected_at IS NULL)" t.index ["school_roll_number"], name: "index_schools_on_school_roll_number", unique: true, where: "(rejected_at IS NULL)" end From 4e3a3993176012aadaba4b59ab324bb9bd24c17c Mon Sep 17 00:00:00 2001 From: Jamie Benstead Date: Tue, 24 Mar 2026 09:33:05 +0000 Subject: [PATCH 6/6] Add test for not returning user if user is creator of rejected school --- spec/models/school_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/models/school_spec.rb b/spec/models/school_spec.rb index 06846060c..873441532 100644 --- a/spec/models/school_spec.rb +++ b/spec/models/school_spec.rb @@ -489,6 +489,14 @@ user = build(:user) expect { described_class.find_for_user!(user) }.to raise_error(ActiveRecord::RecordNotFound) end + + it('raises ActiveRecord::RecordNotFound if the user is the creator of a rejected school') do + creator = create(:user) + school.update!(creator_id: creator.id) + school.reject + + expect { described_class.find_for_user!(creator) }.to raise_error(ActiveRecord::RecordNotFound) + end end describe '#verified?' do