diff --git a/app/models/school.rb b/app/models/school.rb index 2f14500b0..0080abeeb 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 @@ -53,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 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..f7698eb83 --- /dev/null +++ b/db/migrate/20260323152328_update_creator_id_index_on_schools.rb @@ -0,0 +1,20 @@ +class UpdateCreatorIdIndexOnSchools < ActiveRecord::Migration[7.2] + def up + 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 + + 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 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 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