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