Skip to content

Change creator_id to only need to be unique for schools where rejecte…#742

Open
jamiebenstead wants to merge 1 commit intomainfrom
1233-creators-of-previously-rejected-schools-cant-sign-up
Open

Change creator_id to only need to be unique for schools where rejecte…#742
jamiebenstead wants to merge 1 commit intomainfrom
1233-creators-of-previously-rejected-schools-cant-sign-up

Conversation

@jamiebenstead
Copy link
Contributor

Status

What's changed?

  • Update school creation validation so that creator_id can be reused if rejected_at is not nil

Copilot AI review requested due to automatic review settings March 20, 2026 14:15
@cla-bot cla-bot bot added the cla-signed label Mar 20, 2026
@github-actions
Copy link

Test coverage

89.79% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/23346854275

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the School model so a creator_id only needs to be unique among non-rejected schools, enabling a user to create a new school after their previous one was rejected.

Changes:

  • Scope creator_id uniqueness validation to records where rejected_at is nil.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +36 to 41
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
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.
Comment on lines +34 to +38
validates :creator_id,
presence: true,
uniqueness: {
conditions: -> { where(rejected_at: nil) }
}
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
validates :creator_id,
presence: true,
uniqueness: {
conditions: -> { where(rejected_at: nil) }
}
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants