how to raise exception with save!

Excuse my ignorance but why are you using a transaction here for one save when

if @merchant.save
redirect_to :action => :whatever
else

Stay here, render your error, and let the user correct the form

end

sounds like it’d work just perfectly for what you need?

RSL

I thought that might be the case. :slight_smile: I’d go with the rescue exception approach then. The API [http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
] seems to suggest that the transaction will raise an exception that you can rescue. You big hero, you. If you’re trying to determine which of the object.saves failed you could always do something like

begin

saved = []
Merchant.transaction do |t|
@merchant.save!
saved << :merchant
@other_thing.save!
saved << :other_thing
end

Make sure to redirect before the end of the error handling

redirect_to :action => :whatever
rescue

Stay here, render the error, let the user correct and resubmit the form

Check the “saved” variable to see which models got saved correctly.

end

And, yes, you can nest transactions as well.

RSL