Validations Help

In our student management system, We have a District Record, Building Record, and a Student Record

In the original configuration we used a HABTM join table for districts_students and buildings_students

What my ultimate goal here is to make sure a Student MUST have at least one district and one building

I tried to use a validation such as

validates :districts, presence: true
validates :buildings, presence: true

This “seemed” to work with District but not building. I would get buildings is invalid on the save

Changed the relationship to be a has_many through and getting the same issue…

Totally stumped why this is not working… And I think I have tried every combination of association and validation

This is my setup currently…

I have a District table


class District < ActiveRecord::Base
  #has_and_belongs_to_many :students
  has_many :district_students, inverse_of: :district
  has_many :students, through: :district_students

I have a Building Table


class Building < ActiveRecord::Base
  #has_and_belongs_to_many :students
  has_many :building_students, inverse_of: :building
  has_many :students, through: :building_students

I have a Students Table


class Student < ActiveRecord::Base
  has_many :district_students, inverse_of: :student
  has_many :districts, through: :district_students
  has_many :building_students, inverse_of: :student
  has_many :buildings, through: :building_students

  validates :districts, presence: true
  validates :buildings, presence: true

I have a DistrictStudents join table


class DistrictStudent < ActiveRecord::Base
  belongs_to :district
  belongs_to :student

  validates_associated :district
  validates_associated :student
end

and finally I have a BuildingStudents join table


class BuildingStudent < ActiveRecord::Base
  belongs_to :building
  belongs_to :student

  validates_associated :building
  validates_associated :student
end

Any ideas oh Rails gurus ???

John Sanderbeck

Which version of Rails are you on?

Rails 6.0.3 and Ruby 2.7

Well I SWEAR I tried this before, but changing Student to the following seems to have worked…

  has_many :district_students
  has_many :districts, through: :district_students
  has_many :building_students
  has_many :buildings, through: :building_students

  # Added because a student MUST be in at least one building and one district 5-15-2020
  validates :buildings, length: { minimum: 1 }
  validates :districts, length: { minimum: 1 }

John

It seems like validates_presence should work. I wonder if it’s a caching issue. You might need to call reload on student before validating the presence of the association. You can set up an executable test case to narrow down any issues https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_gem.rb

the following seems to have worked

Great! The docs indicate that you’ll also want to use validates_associated :buildings to ensure that the building model is valid ActiveRecord::Validations::ClassMethods

I have the validates_associated in the joins tables. Is that where they should be?

One issue I see with this is if I don’t pick a district I get two errors. One that the district count is less than one and I also get that district_students is invalid…

John