Rails的:异常的after_create停止保存 [英] Rails: Exception in after_create stopping save
问题描述
简单的问题。我有,我想之后保存记录进行后处理的ActiveRecord模型。因此,在模型中我有坚持工作到我Resque队列queue_for_processing方法。为了使这个执行后,我的记录被成功地坚持我已经在我的模型写入以下内容:
Simple question. I have a ActiveRecord model that I want to perform post processing on AFTER the record is saved. So in the model I have a queue_for_processing method that sticks a job onto my Resque queue. To make this execute after my record is successfully persisted I have written the following in my model:
after_create :queue_for_processing
pretty的简单。我原以为预期EXCEPT昨晚我Redis的服务器宕机,事情走到歪了一切工作。我的期望是,该记录将仍然被保存,我以后可以手动处理作业。但queue_for_processing方法抛出异常(预期行为),并从节约停止记录。
Pretty simple. I had thought that everything was working as expected EXCEPT that last night my redis server went down and things went awry. My expectations were that the record would still be saved and I could process the job later manually. But the queue_for_processing method is throwing an exception (expected behavior) and stopping the record from saving.
我误解了如何after_create的作品?或者是我的理解正确,一些时髦的情况?
Am I misunderstanding how after_create works? Or is my understanding correct and something funky happening?
感谢。
推荐答案
是的,回调都是在一个事务包裹起来。
Yes, the callbacks are all wrapped up in a transaction.
基本上,以下将导致回退:
Basically, the following will cause a rollback:
- 从
before_save
返回false或类似的回调 - 异常
before_save
或类似的回调 - 异常
after_save的
或类似的回调(after_create)
- return false from
before_save
or similar callbacks - exception in
before_save
or similar callbacks - exception in
after_save
or similar callbacks (after_create)
下面不会导致回滚:
- 从
after_save的
返回false或类似的回调 - 异常
after_commit
- return false from
after_save
or similar callbacks - exception in
after_commit
如果你不想异常引起回滚,使用 after_commit
If you don't want an exception to cause a rollback, use after_commit
- 参考:<一href="http://webonrails.com/2012/08/28/activerecord-after_commit-hook/">http://webonrails.com/2012/08/28/activerecord-after_commit-hook/
- 附加参考:<一href="http://guides.rubyonrails.org/v3.1.3/active_record_validations_callbacks.html#transaction-callbacks">http://guides.rubyonrails.org/v3.1.3/active_record_validations_callbacks.html#transaction-callbacks
- Reference: http://webonrails.com/2012/08/28/activerecord-after_commit-hook/
- Additional reference: http://guides.rubyonrails.org/v3.1.3/active_record_validations_callbacks.html#transaction-callbacks
这篇关于Rails的:异常的after_create停止保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!