为什么这段代码不能以事务方式运行?
我想销毁与特定用户有关的所有问题。但有些问题受到保护,可以防止自身被破坏并引发异常。
我希望以下代码能够销毁所有问题或不销毁任何问题,但事实并非如此 - 如果其中存在受保护的问题,它不会回滚之前的销毁操作 - 这是为什么?
class User < ActiveRecord::Base
...
Questions.transaction do
# protected questions will raise a runtime exception
Questions.destroy_all(:user_id => self.id)
end
end
I want to destroy all the questions to do with a particular user. Some questions are protected though and may prevent themselves from being destroyed and raise an exception.
I would expect the following code to destroy either all questions or none however it doesn't - if there is a protected question amongst the others it doesn't roll back the previous destroy actions - why is this?
class User < ActiveRecord::Base
...
Questions.transaction do
# protected questions will raise a runtime exception
Questions.destroy_all(:user_id => self.id)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Grrr,刚刚意识到我以前遇到过这个问题,并且在弄清楚之前浪费了很多时间。
问题是测试是在 RSpec 中完成的,RSpec 本身使用事务并因此从代码中删除事务功能(注意任何从 RSpec 团队阅读此内容的人 - 在解析包含事务的代码时出现警告会很棒 -蒂!)。
为了使事务在 RSpec 中工作,将其包装在以下代码中
:
Grrr, just realised I'd hit this problem before and wasted a load of time then before figuring it out.
The issue is that the test is being done in RSpec which itself uses transactions and removes transactional functionality from the code as a result (n.b. anyone reading this from the RSpec team - it would be great to have a warning when parsing code that contains transactions - ty!).
In order to make the transaction work within RSpec wrap it in the following code:
end