Forum: Ruby on Rails Noob: Validation Errs Don't Display When Saving Object Tree

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
71a6acd64a1e2e3c7577c9ee8c904f7b?d=identicon&s=25 donncha (Guest)
on 2006-05-23 03:34
I've got the following model:

User < AR::Base
  has_one :user_detail

UserDetail < AR::Base
  belongs_to :user
  has_one :address

Address < AR::Base
  belongs_to :user_detail

I've a form to gather the data, and it includes partials for each class.
Each partial has an error_messages_for 'class' snippet which should
display the errors relevant to each class.
On submission I'm doing:

@user = User.new(params[:user])
@user.user_detail = UserDetail.new(params[:user_detail])
@user.user_detail.address = Address.new(params[:address])

If I complete the form, all is well and the object tree gets saved
correctly to the DB. However, if I deliberately omit key information
from the form, only errors relevant to the User class are displayed. The
User.save succeeds even if UserDetail or Address validations fail.

I thought that a save would fail if any of the objects on the model tree
failed validation, and that all saves are transactional, so if one
element failed the whole object tree got rolled back?

How can I ensure that
a) a validation failure in either UserDetail or Address causes a
rollback of the save
b) validation errors in UserDetail and/or Address get displayed in the
form?

thanks,
donncha
This topic is locked and can not be replied to.