Skip to content

Commit c87d1ac

Browse files
committed
Add uniqueness constraint to School#creator_id
At present we don't want a user to be able to create more than a single school (although we have hopes of relaxing this constraint in future). I've checked the staging and production databases for existing violations of this constraint and it looks like we're OK: there are 5 schools in staging, each with a unique creator_id, and there's only a single school in production.
1 parent bcbc420 commit c87d1ac

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

app/models/school.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class School < ApplicationRecord
1414
validates :municipality, presence: true
1515
validates :country_code, presence: true, inclusion: { in: ISO3166::Country.codes }
1616
validates :reference, uniqueness: { case_sensitive: false, allow_nil: true }, presence: false
17-
validates :creator_id, presence: true
17+
validates :creator_id, presence: true, uniqueness: true
1818
validates :creator_agree_authority, presence: true, acceptance: true
1919
validates :creator_agree_terms_and_conditions, presence: true, acceptance: true
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddUniqueConstraintToSchoolCreatorId < ActiveRecord::Migration[7.0]
2+
def change
3+
add_index :schools, :creator_id, unique: true
4+
end
5+
end

db/schema.rb

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/models/school_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@
111111
expect(school).to be_invalid
112112
end
113113

114+
it 'requires a unique creator_id' do
115+
school.save!
116+
another_school = build(:school, creator_id: school.creator_id)
117+
another_school.valid?
118+
expect(another_school.errors[:creator_id]).to include('has already been taken')
119+
end
120+
114121
it 'rejects a badly formed url for website' do
115122
school.website = 'http://.example.com'
116123
expect(school).to be_invalid

0 commit comments

Comments
 (0)