# Child validations?

**URL:** <https://www.ruby-forum.com/t/child-validations/105942>\
**Category:** Rails\
**Created:** [June 4, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/child-validations/105942 "2007-06-04T21:04:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [June 4, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/child-validations/105942/1 "2007-06-04T21:04:02Z")

</div>

Hi,

I’m working on my User model, which derives from Party, and parties have  
many e-mail addresses, telephones, and mailing addresses. When I save  
the user model, the validations occur, but the child error messages  
appear to be summarized. I expected the child errors to be listed in  
detail, as in “Put in the address, dummy”. Instead, rails returns  
“Emails is invalid”, or “Addresses is Invalid” for the message.

In irb, I see that there are some interesting methods for the user  
object:  
validate\_associated\_records\_for\_emails  
validate\_associated\_records\_for\_addresses  
validate\_associated\_records\_for\_phones

They appear to add the child messages to the @errors object of the user  
model, but they don’t seem to be called by @user.save!

Also, I tried adding the following to the view, it didn’t appear to  
help:  
\<%= error\_messages\_for :address %\>  
\<%= error\_messages\_for :email %\>  
\<%= error\_messages\_for :phone %\>

What is the rails way to force the display of the child errors? Or  
perhaps more appropriately, what is the correct way to handle validation  
when working with parent and child records on the same form.

Thank you!

app/models/party.rb  
class Party \< ActiveRecord::Base  
has\_many :addresses  
has\_many :emails  
has\_many :phones  
end

app/models/user.rb  
Class User \< Party  
end

app/models/email.rb  
class Email \< ActiveRecord::Base  
belongs\_to :party  
validates\_presence\_of :address, “Put in the address, dummy”  
end

app/controllers/account\_controller.rb  
def signup  
@user = User.new  
@user.emails \<\< Email.new  
@user.phones \<\< Phone.new  
@user.addresses \<\< Address.new  
end

def signup\_post  
@user = User.new(params[:user])  
params[:email].each {|e| @user.emails \<\< Email.new(e)}  
params[:address].each {|a| @user.addresses \<\< Address.new(a)}  
params[:phone].each {|t| @user.phones \<\< Phone.new(t)}  
@user.save!  
flash[:notice] = “Thanks for signing up!”  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘signup’  
end

Regards,  
Rich

---

<div class="post-metadata">

**Author:** ![DuzenburyS\_Rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/43a26b/32.png) [@DuzenburyS\_Rich](https://www.ruby-forum.com/u/DuzenburyS_Rich)\
**Post date:** [June 6, 2007, 5:20am UTC](https://www.ruby-forum.com/t/child-validations/105942/2 "2007-06-06T05:20:56Z")

</div>

Duzenbury, Rich wrote:

> validate\_associated\_records\_for\_addresses  
> validate\_associated\_records\_for\_phones
> 
> They appear to add the child messages to the @errors object of the user  
> model, but they don’t seem to be called by @user.save!

Those built-in child validations will only be done for new  
children, not for child updates. Use a validates\_associated  
declaration to fully control when child validity is checked.  
You can then turn off the automatic validation by removing or  
emptying the above methods.

> Also, I tried adding the following to the view, it didn’t appear to  
> help:  
> \<%= error\_messages\_for :address %\>  
> \<%= error\_messages\_for :email %\>  
> \<%= error\_messages\_for :phone %\>
> 
> What is the rails way to force the display of the child errors? Or  
> perhaps more appropriately, what is the correct way to handle validation  
> when working with parent and child records on the same form.

The most simple way to display all the errors:

\<% for assn in [:addresses, :emails, :phones]  
for @obj in @user.send(assn) %\>  
\<%= error\_messages\_for :obj %\>  
\<% end  
end %\>

–  
We develop, watch us RoR, in numbers too big to ignore.
