# Insert Records in Multiple Tables with One Form

**URL:** <https://www.ruby-forum.com/t/insert-records-in-multiple-tables-with-one-form/120164>\
**Category:** Rails\
**Created:** [November 14, 2007, 11:14am UTC](https://www.ruby-forum.com/t/insert-records-in-multiple-tables-with-one-form/120164 "2007-11-14T11:14:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![burttonboarder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@burttonboarder](https://www.ruby-forum.com/u/burttonboarder)\
**Post date:** [November 14, 2007, 11:14am UTC](https://www.ruby-forum.com/t/insert-records-in-multiple-tables-with-one-form/120164/1 "2007-11-14T11:14:31Z")

</div>

I’m currently working on my first rails project and I’m running into a  
frustrating problem. I have two tables, one called People and one called  
Addresses, with person and address active record models respectively.

I’ve created form which allows the user to enter in some personal  
information, as well as add address information. When the form is  
submitted it saves a person a record, and it saves an address record.  
I’m using has\_many :addresses in my person model.

The code that I’m using in my controller for this action is the  
following

@user = User.new(params[:user])  
@user.person = Person.new(params[:person])  
@user.person.phone\_numbers \<\< PhoneNumber.new(params[:phone\_numbers])  
@user.person.addresses \<\< Address.new(params[:address])  
if !@user.save  
render(:action =\> “newUser”)  
end

In my newUser view I am using the error\_message\_on method to display  
error message for each one of my form fields.

The problem that I’m having is that if an error occurs when trying to  
save the address model the user still gets saved. I was able to fix this  
by adding the validates\_associated :addresses to my person model, but  
this gives a very useless error message stating that Addresses is  
invalid. Instead I’d still like to see specifically which fields in  
address are causing the validation error to occur.

Please help.

Thank you.
