I’ve got the following model:
User < AR::Base
UserDetail < AR::Base
Address < AR::Base
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