Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/models/school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Comment on lines +34 to +38
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowing multiple schools per creator_id (when prior ones are rejected) makes School.find_for_user! ambiguous: it currently does find_by(creator_id: user.id) without preferring the active (non-rejected) school. This can return an older rejected school, causing /api/my_school to surface the wrong record. Consider scoping the lookup to rejected_at: nil (or otherwise selecting the most relevant school deterministically).

Copilot uses AI. Check for mistakes.
Comment on lines +34 to +38
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are specs covering reuse for reference, district_nces_id, and school_roll_number when a school is rejected, but no equivalent coverage for the new creator_id reuse rule. Add a model spec that rejects the original school and asserts a second school with the same creator_id is valid/saves (and ideally a spec asserting find_for_user! returns the non-rejected one when multiple exist).

Copilot uses AI. Check for mistakes.
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
Comment on lines +36 to 41
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model validation now scopes creator_id uniqueness to rejected_at: nil, but the database still enforces a global unique index on schools.creator_id (index_schools_on_creator_id is unique in db/schema.rb). As a result, creating a new school for a creator whose previous school was rejected will still fail at the DB layer. Add a migration to replace the existing unique index with a partial unique index (WHERE rejected_at IS NULL) (and ensure the index name is stable / removal is idempotent).

Suggested change
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
uniqueness: true
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
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
validates :creator_agree_responsible_safeguarding, presence: true, acceptance: true

Copilot uses AI. Check for mistakes.
Expand Down
Loading