I'm working on a rails project where I have a table called people and a table called addresses.
Each person can have many addresses. I've created a form which includes fields from both the People table and the Addresses table, and should create a new record in both tables upon successful submission. The problem that I'm having is that if the User record passes validation the address record does not, the user record still gets saved.
The code in my controller looks like this:
-
@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
-
I'm trying to use the validates_associated :addresses in the person model, but the error message that I get from that is pretty useless. It just says Addresses is invalid. I'm using the error_message_on method in my view for this page and would like to see what fields belonging to the address table are invalid, instead of that dumb generic message.
PLEASE HELP, I'd really appreciate it.