# Proper use of validates\_associated

**URL:** <https://www.ruby-forum.com/t/proper-use-of-validates-associated/67426>\
**Category:** Rails\
**Created:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/proper-use-of-validates-associated/67426 "2006-09-04T14:43:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/proper-use-of-validates-associated/67426/1 "2006-09-04T14:43:37Z")

</div>

Hi All,

Is anyone using validates\_associated in a productive manner?  
What I mean is that I have not figured out how validates\_associated is  
actually supposed to make life easier.

I have been playing with this a little but have not gotten much out if  
it. For example I have a Person model and an Activation model. In the  
Person model I have defined validates\_associated :activations (Person  
has a 1…n with Activation)

When I do this for example after attempting person.save

\<%= error\_messages\_for ‘person’ %\>

and there is something invalid in an Activation instance associated  
with the person I simply get an error message (actually I get it twice,  
not sure why yet) stating that “Activations is invalid” rather than the  
specific error for the attribute which failed validation in the  
Activation instance. This is not very useful.

Alternatively I can of course validate the person instance and  
activation instance independantly and then dig out the  
error\_messages\_for for each instance but this is a little redundant.

Anyone have any good example or some good reading which discusses  
validates\_associated in more detail?

Thanks,  
Serge

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/proper-use-of-validates-associated/67426/2 "2006-09-04T14:43:38Z")

</div>

I’m doing something very similar and am using this:

[http://railtie.net/articles/2006/01/26/enhancing\_rails\_errors](http://railtie.net/articles/2006/01/26/enhancing_rails_errors)

to report errors in a single error\_messages\_for report. However, it is  
still showing “ModelName is invalid” as well as the other error  
messages, which while not ideal will have to do at the moment!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 11, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/proper-use-of-validates-associated/67426/3 "2006-09-11T18:36:42Z")

</div>

On Sunday 03 September 2006 14:15, removed\_email\_address@domain.invalid wrote:

> When I do this for example after attempting person.save
> 
> \<%= error\_messages\_for ‘person’ %\>
> 
> and there is something invalid in an Activation instance associated  
> with the person I simply get an error message (actually I get it  
> twice, not sure why yet) stating that “Activations is invalid” rather  
> than the specific error for the attribute which failed validation in  
> the Activation instance. This is not very useful.

On Sunday 03 September 2006 15:00, Alastair M. wrote:

> I’m doing something very similar and am using this:
> 
> [http://railtie.net/articles/2006/01/26/enhancing\_rails\_errors](http://railtie.net/articles/2006/01/26/enhancing_rails_errors)
> 
> to report errors in a single error\_messages\_for report. However, it  
> is still showing “ModelName is invalid” as well as the other error  
> messages, which while not ideal will have to do at the moment!

See ActiveRecord::Associations#add\_multiple\_associated\_save\_callbacks  
This method adds validations for all associated objects. If you add your  
own validation with validates\_associated you get double validation –  
and double error messages – for the same thing.

I only found out today myself.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
