# Some newbie help needed on validating a nested form/model

**URL:** <https://www.ruby-forum.com/t/some-newbie-help-needed-on-validating-a-nested-form-model/147691>\
**Category:** Rails\
**Created:** [September 4, 2008, 9:29am UTC](https://www.ruby-forum.com/t/some-newbie-help-needed-on-validating-a-nested-form-model/147691 "2008-09-04T09:29:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [September 4, 2008, 9:29am UTC](https://www.ruby-forum.com/t/some-newbie-help-needed-on-validating-a-nested-form-model/147691/1 "2008-09-04T09:29:53Z")

</div>

Been googling and coming up short on finding examples on the best  
practice for validating nested forms. I’m still a newb so any concrete  
code examples would be great.

My application is going to allow one to record hockey matches between  
a home and an away player.

I have a “Match” model which holds some basic match details (date,  
period length, etc.)

Each “Match” also contains a collection (two) “PlayerGame” model  
objects. Each of these objects holds the statistics of that person’s  
game (ie score, goals, faceoffs won, etc.) Each has a ‘match\_id’ tying  
it to the Match model.

The UI should let you perform on CRUD on a match while letting you  
enter the game details for the two player game models that make up the  
match. This all works fine. It’s validating the nested “PlayerGame”  
objects where I’m having the trouble.

I’d like to be able set up validations on the “PlayerGame” model and  
call them… even if manually… .from the Match controller. Using  
“validates\_associated :player\_games” in my Match model ‘sort of’  
works, but the resulting display messages are all wrong… you only end  
up with a generic message “Player games is invalid” for each invalid  
call.

Are there some examples of what I need to do?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [September 4, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/some-newbie-help-needed-on-validating-a-nested-form-model/147691/2 "2008-09-04T17:35:08Z")

</div>

Let me give a follow up example with a more simplified example…

Take a case where you have a “Person” model and a Person can have  
several “Address” model objects.

You present a simple Person form and you provide fields of “address1”  
and “address2” just as a starting point.

When the form submits you want to make sure the Address objects  
validate correctly. (CRUD for the stuff I have working just fine.)

I’d like to keep validations on the Address object. For example…

class Address \< ActiveRecord::Base  
validates\_numericality\_of :zip\_code (yes I know a validation  
would be more than that for a zip code, but just as an example.)

Now in …

class Person \< ActiveRecord::Base  
validate :my\_custom\_validation\_of\_addresses

private  
def my\_custom\_valiation\_of\_addresses  
addresses.each do | address |  
#??? 2 ???

in 2 above, how do I collect errors from the Address object that can  
utilize the validation rules already established on Address. For  
example, shouldn’t there be a way I could leverage the  
“validates\_numericality\_of” that is already defined on Address?

Of course in my Person model I could add ‘validates\_associated  
:addresses’ but that doesn’t work since the resulting error messages  
are now all generic looking with just “Addresses is invalid” for each  
failed validation on Address. I don’t want to duplicate validation  
code (stay DRY), so I’m wondering what the best practice is. In theory  
Address should be able to take care of its own way of validating  
itself since you never know when it will need it to be validated  
stand-alone (not part of a Person model.) Is the best practice to  
create a more verbose validation method on the Address object and call  
that as you iterate over addresses in person ? Should I just create  
an Address model method:

def validate\_myself  
#can I now call validate:

I was thinking in the above I could then maybe manually call…  
validate\_numericality\_of but that doesn’t seem to work

Thanks for any help. I guess what I’d like to see is a best practice  
of doing things the ‘rails way’ of a code snippet of where the  
validation would be placed so that things are DRY. I feel like I must  
be missing something obvious since I can’t see how this doesn’t come  
up more in real life.

(Original post below…)

On Wed, Sep 3, 2008 at 11:05 PM, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Each “Match” also contains a collection (two) “PlayerGame” model  
> call them… even if manually… .from the Match controller. Using  
> “validates\_associated :player\_games” in my Match model ‘sort of’  
> works, but the resulting display messages are all wrong… you only end  
> up with a generic message “Player games is invalid” for each invalid  
> call.
> 
> Are there some examples of what I need to do?
> 
> Thanks for any help.

–  
Rick
