# Issues with validates\_associated not throwing error

**URL:** <https://www.ruby-forum.com/t/issues-with-validates-associated-not-throwing-error/72149>\
**Category:** Rails\
**Created:** [October 14, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/issues-with-validates-associated-not-throwing-error/72149 "2006-10-14T21:08:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mike\_Adam](https://www.ruby-forum.com/u/Mike_Adam)\
**Post date:** [October 14, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/issues-with-validates-associated-not-throwing-error/72149/1 "2006-10-14T21:08:44Z")

</div>

Hello all

I’m having an issue with ruby on rails, and it not throwing an error  
where it should.

I have a class ‘clientpool’ that is:  
class Clientpool \< ActiveRecord::Base

set\_table\_name “clientpool”  
set\_primary\_key “id”

belongs\_to :clients, :foreign\_key =\> “cliname”

validates\_presence\_of :cliname  
validates\_associated :client  
end

However, when I try to make a new clientpool, that has an invalid  
cliname, that is not listed in clients, it will still create it and not  
error out.

I see in the output log it does run the SQL to check, and the SQL  
querry returns 0 rows, so it should error out, but it doesn’t:  
SQL (0.000088) BEGIN  
Client Load (0.000115) SELECT \* FROM clients WHERE (clients.id =  
444) LIMIT 1  
SQL (0.000279) INSERT INTO clientpool (`cliname`, `prefix`, `did`)  
VALUES(444, 0, 0)  
SQL (0.000072) COMMIT

Anyone got any ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![Mike\_Adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mike\_Adam](https://www.ruby-forum.com/u/Mike_Adam)\
**Post date:** [October 15, 2006, 11:01am UTC](https://www.ruby-forum.com/t/issues-with-validates-associated-not-throwing-error/72149/2 "2006-10-15T11:01:52Z")

</div>

On Oct 14, 2006, at 21:07, Mike Adam wrote:

> I’m having an issue with ruby on rails, and it not throwing an error  
> where it should.  
> [SNIP]  
> However, when I try to make a new clientpool, that has an invalid  
> cliname, that is not listed in clients, it will still create it and  
> not error out.

I think you’ve misunderstood what the various validations do.

> validates\_presence\_of :cliname

All this does is make sure the cliname attribute is present on the  
clientpool object, ie is not blank

> validates\_associated :client

This ensures that the associated client object is valid, ie is  
passing its own validations.

And another thing:

> belongs\_to :clients, :foreign\_key =\> “cliname”

You probably want belongs\_to :client, not :clients, since the model  
can only belong to a single client.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![Mike\_Adam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/958977/32.png) [@Mike\_Adam](https://www.ruby-forum.com/u/Mike_Adam)\
**Post date:** [October 16, 2006, 1:25am UTC](https://www.ruby-forum.com/t/issues-with-validates-associated-not-throwing-error/72149/3 "2006-10-16T01:25:35Z")

</div>

Jakob S. wrote:

> This ensures that the associated client object is valid, ie is  
> passing its own validations.
> 
> And another thing:
> 
> > belongs\_to :clients, :foreign\_key =\> “cliname”
> 
> You probably want belongs\_to :client, not :clients, since the model  
> can only belong to a single client.

I appreciate your changes, I did make them, it still does the same SQL,  
but still is allowing invalid :cliname to be created that don’t link to  
a :client

How am I to alter how it validates a :client so that it will ensure  
only valid ones are there?

class Clientpool \< ActiveRecord::Base

set\_table\_name “clientpool”  
set\_primary\_key “id”

belongs\_to :client, :foreign\_key =\> “cliname”

validates\_associated :client, :on =\> :create  
validates\_presence\_of :cliname

end
