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
You must use a database engine that supports ACID transactions. For mysql that is INNODB.
show table status\G
If users or companies is not using InnoDB engine, you can change it w/ this command.
ALTER TABLE <table name> ENGINE INNODB;
the exception thrown from @company.save!
*should trigger a ROLLBACK command to be sent to the database. you can verify this in the console/logfile when running script/server with DEBUG log level.
attempting to save a new entry and revise an existing entry (based on the new entry) at the same time, ran into a similar problem. Tried transaction with rescue failed validation, but settled on this instead:
if @new_entry.valid? && @existing_entry.valid?
ActiveRecord::Base.transaction do
@new_entry.save!
@existing_entry.save!
end
end
the code validates first. it doesn't attempt to save unless both entries are valid. transaction semantics guard against incomplete entry on other errors if the database supports it. hope that's a good solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With