【发布时间】:2015-04-23 07:59:56
【问题描述】:
在我的控制器中,我有一些类似...的代码
...
if user.save
something = Something.where("thing = ?", thing)
if !(something.nil?)
render json: { something: something }
else
#I WOULD LIKE TO ROLLBACK THE user.save HERE
end
else
render json: { error: user.errors.full_messages }, status: :bad_request
end
我试过了
raise ActiveRecord::Rollback, "Could not create new User, Something was not found."
render json: { error: "Could not create new User, Something was not found"}, status: :unprocessable_entity
代替上面的 ROLLBACK COMMENT 区域,但这不起作用。 user.save 最终通过。它向'rails s'吐出一些东西,但它不会回滚最后一个事务。
【问题讨论】:
-
这个
Something到底是什么?它与用户有什么关系吗?
标签: ruby-on-rails activerecord rollback