@@ -17,6 +17,7 @@ class School < ApplicationRecord
1717 validates :municipality , presence : true
1818 validates :country_code , presence : true , inclusion : { in : ISO3166 ::Country . codes }
1919 validates :reference , uniqueness : { case_sensitive : false , allow_nil : true } , presence : false
20+ validates :district_nces_id , uniqueness : { allow_nil : true } , presence : false
2021 validates :creator_id , presence : true , uniqueness : true
2122 validates :creator_agree_authority , presence : true , acceptance : true
2223 validates :creator_agree_terms_and_conditions , presence : true , acceptance : true
@@ -32,6 +33,7 @@ class School < ApplicationRecord
3233 validate :code_cannot_be_changed
3334
3435 before_validation :normalize_reference
36+ before_validation :normalize_district_fields
3537
3638 before_save :format_uk_postal_code , if : :should_format_uk_postal_code?
3739
@@ -94,6 +96,12 @@ def normalize_reference
9496 self . reference = nil if reference . blank?
9597 end
9698
99+ # Ensure district fields are nil, not empty strings
100+ def normalize_district_fields
101+ self . district_name = nil if district_name . blank?
102+ self . district_nces_id = nil if district_nces_id . blank?
103+ end
104+
97105 def verified_at_cannot_be_changed
98106 errors . add ( :verified_at , 'cannot be changed after verification' ) if verified_at_was . present? && verified_at_changed?
99107 end
0 commit comments