Considering an object with several has_many :through => associations,
what is the ‘best’ way to handle validations?
As an example:
class Student < ActiveRecord::Base
some attrbutes like
:name
:grade
relationships
has_many :students_assignment, :dependent => :destroy
has_many :assignments, :through => :students_assignment
has_many :students_class, :dependent => :destroy
has_many :classes, :through => :students_class
has_many :students_extracurricular_activity, :dependent => :destroy
has_many :extracurricular_activities, :through
=> :students_extracurricular_activity
has_many :students_school, :dependent => :destroy
has_many :students_schools, :through => :students_school
end
Suppose a student can’t belong to a class and extracurricular activity
that occur at the same time? I figure that this validation would be
best handled in the Student class since the other case would require
writing validations for both classes and eca’s. Also, this is just an
example. So, what if a student also can only take a :class through
multiple :schools if they are of a certain :grade and only 5 students
can belong to each eca?
I’m trying to illustrate a complex situation where the combination of
multiple associations affect the validity. So either each association
has to be validated, which means processing for the most part the same
validations for all associations. For instance, checking a students
eca’s against the students classes (for timing conflicts) would be
done twice, once from eca’s and again from classes. i would like to
handle this through a validates_associated so that the Student is
validated whenever an association is changed or added. However, I
can’t see how this would work.
To illustrate:
ss = StudentsSchool.new(:school_id => 1, :student_id => 10)
=> #…ss.student
=> #…ss.student.students_school
=> []
So the ss is still a temporary object that won’t be seen in the
Student validation.
Can anyone share some ideas?