# Validations not work fine with associations

**URL:** <https://www.ruby-forum.com/t/validations-not-work-fine-with-associations/115600>\
**Category:** Rails\
**Created:** [September 26, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/validations-not-work-fine-with-associations/115600 "2007-09-26T18:32:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [September 26, 2007, 6:32pm UTC](https://www.ruby-forum.com/t/validations-not-work-fine-with-associations/115600/1 "2007-09-26T18:32:18Z")

</div>

Hello,

I thiks that rails validations not work fine with associations. For  
example, if we have the models Antenna, Area and Position, where  
positions are grouped by areas, areas has many antennas and antennas can  
have positions. I want to validate that only antennas with an area can  
be associated with positions, ie:

class Antenna \< ActiveRecord::Base  
belongs\_to :area  
has\_and\_belongs\_to\_many :positions

def validate  
if area.nil? and positions.size \> 0  
errors.add(:positions, “The antenna must to…”)  
end  
end  
end

But, although the validation send the error, the association is  
performed:

> > p = Position.find(68)  
> > p.antennas = []  
> > =\> []
> 
> > a = Antenna.find(:first, :conditions =\> “area\_id is null”)  
> > a.positions \<\< p  
> > a.save  
> > =\> false
> 
> > p = Position.find(68)  
> > p.antennas.size  
> > =\> 1
> 
> > p.antennas[0].id  
> > =\> 13
> 
> > a.id  
> > =\> 13

There is a way for validate it? I think that it must to work with  
scaffolding that manage associations (like Active Scaffold).

Daniel
