Insert Records in Multiple Tables with One Form

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.