I have two models: user and company. They both get created from one form and I'm using a transaction like this:
User.transaction do
@user.save!
@company.user = @user
@company.save!
@user.reload
@user.company = @company
@user.save!
flash[:notice] = "Thank you for your registration."
redirect_to_index
end
The user gets saved to the database even when one of the company's validations fails. I've tried adding explicit error handling of ActiveRecord::RecordInvalid but it didn't help. I thought the validation would raise the error to rollback the transaction anyway. Any help is greatly appreciated.
Thanks