# Validations with HABTM relationships

**URL:** <https://www.ruby-forum.com/t/validations-with-habtm-relationships/69244>\
**Category:** Rails\
**Created:** [September 19, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/validations-with-habtm-relationships/69244 "2006-09-19T17:40:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [September 19, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/validations-with-habtm-relationships/69244/1 "2006-09-19T17:40:22Z")

</div>

I have a couple questions regarding validation.

I have a table with a HABTM relationship to another table. The table  
is “task” and it has HABTM “employee\_types”. Is there any way to have  
a validation to make sure when a task is created it is associated with  
at least one “employee\_type”? I could include code to make sure of  
this in the controller method but I’m wondering there is a built rails  
way, or a way to stick a validation method into the “task” model.

a “task” also has the following relationship defined:

has\_many :work\_area\_tasks, :dependent =\> :destroy, :order =\> :position  
has\_many :work\_areas, :through =\> :work\_area\_tasks

Again, I’d like to make sure at least one “work\_area\_task” is created  
with each “task”.

–  
Sterling A.  
sterling [at] [sterlinganderson.net](http://sterlinganderson.net)  
[http://sterlinganderson.net/](http://sterlinganderson.net/)  
608.239.8387

---

<div class="post-metadata">

**Author:** ![Sterling\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Sterling\_A](https://www.ruby-forum.com/u/Sterling_A)\
**Post date:** [September 19, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/validations-with-habtm-relationships/69244/2 "2006-09-19T18:03:00Z")

</div>

Sterling A. wrote:

> I have a table with a HABTM relationship to another table. The table  
> is “task” and it has HABTM “employee\_types”. Is there any way to have  
> a validation to make sure when a task is created it is associated with  
> at least one “employee\_type”? I could include code to make sure of  
> this in the controller method but I’m wondering there is a built rails  
> way, or a way to stick a validation method into the “task” model.

Unless there is a good reason to stay with habtm, I’d say you should  
shift to using a join model with has\_many :through. Then you’ll have a  
model class to put the validation in.

> a “task” also has the following relationship defined:
> 
> has\_many :work\_area\_tasks, :dependent =\> :destroy, :order =\> :position  
> has\_many :work\_areas, :through =\> :work\_area\_tasks
> 
> Again, I’d like to make sure at least one “work\_area\_task” is created  
> with each “task”.

You need to create/save the task before you can create a join model  
record associating it to a work area, so you can’t use vanilla  
validations. You’ll have to find another place in your workflow to  
validate that. If you always create a task with an associated work\_area,  
you could make a Task class method that creates all the records required  
together in a transaction and validates them as a set. You can also put  
a before\_destroy callback in the join model to check if it is the only  
record with its task\_id.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
