In my rails app, I have a transaction block in a controller's create action in which I attempt to create two records.
def create
ActiveRecord::Base.transaction do
@foo = Foo.new(params[:foo].except(:bar))
raise ActiveRecord::Rollback unless @foo.save
@bar = Bar.new(params[:foo][:bar])
raise ActiveRecord::Rollback unless @bar.save
end
end
I'd like to be able to rescue from the Rollback so I can return an error indicating which save failed.
rescue ActiveRecord::Rollback => e
@foo.errors = e if @foo.errors.blank?
ensure
respond_with @foo
end
However, I never get into the rescue block. I assume this is because, as the rails documentation states, the transaction block eats the Rollback exception and doesn't re-raise it. Is there a way to force this exception to be re-raised?