# Validating an ActiveRecord object and its has\_many :through associations

**URL:** <https://www.ruby-forum.com/t/validating-an-activerecord-object-and-its-has-many-through-associations/134533>\
**Category:** Rails\
**Created:** [April 11, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/validating-an-activerecord-object-and-its-has-many-through-associations/134533 "2008-04-11T19:33:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_M\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@John\_M\_L](https://www.ruby-forum.com/u/John_M_L)\
**Post date:** [April 11, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/validating-an-activerecord-object-and-its-has-many-through-associations/134533/1 "2008-04-11T19:33:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![John\_M\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@John\_M\_L](https://www.ruby-forum.com/u/John_M_L)\
**Post date:** [April 11, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/validating-an-activerecord-object-and-its-has-many-through-associations/134533/2 "2008-04-11T23:03:24Z")

</div>

With this complexity, you are best off (IMNSHO) rolling your own  
validation code. In the Student model, just override the “validate”  
method:

def validate  
… do all your logic here, e.g. …  
errors.add(“student”, “this student can’t be in that class and that  
activity”) if XYZ  
end

Then, you set the errors hash (or array? I forget what data structure  
it is) if anything goes wrong.

I’d start by pseudo-coding it in plain english to be sure you’ve  
articulated your association constraints. That call is on the model,  
so you get all the association goodness, like if a student has\_many  
activities, you can do: self.activities in that call to reference the  
activities association for the student instance being validated.

Remember, get the logic working first, then refactor later. I used to  
have a lot of trouble with this. I’d spend an hour mentally figuring  
out the “perfect” way to capture and solve a specific problem instead  
of doing “good enough” in 3 minutes, then refactoring the next day for  
5 minutes.

Good luck!

-Danimal

---

<div class="post-metadata">

**Author:** ![John\_M\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90db22/32.png) [@John\_M\_L](https://www.ruby-forum.com/u/John_M_L)\
**Post date:** [April 11, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/validating-an-activerecord-object-and-its-has-many-through-associations/134533/3 "2008-04-11T23:37:56Z")

</div>

Thanks Danimal.

I’m about to do the same. My main issue is figure out how to validate  
with associated objects that only exist in memory. I guess I have to  
actually attach them to the main (Student) object and then validate.

John
