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