回形针,记录先于文件保存到磁盘上保存
我正在上传图像,并且创建新记录时会触发观察者。在观察者中,我通过 API 将图像推送到其他服务。 问题是在本地或在 rspec 测试中执行此操作时,似乎在将图像保存在磁盘上之前将记录保存在数据库中,并且我收到文件未找到错误。如何使其保持正确的顺序?
I'm uploading an image and there is observer which is triggered when new record is created. In observer I'm pushing image via API to other service.
Problem is when doing it locally or in rspec test, seems that record is saved in DB before image is saved on disk and I'm getting file not found error. How to make it to keep correct order ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
观察者在模型中定义的回调之前被触发。因此,在运行回形针的
after_save
方法处理附件之前,会调用观察者的after_create
方法。考虑使用
after_commit
或ar_after_transaction
而不是after_save
等。当与外部 API 通信时,这就是您想要的,因为否则 API 调用不能如果事务回滚则撤消。Observers are triggered before callbacks defined in the model. Therefore the
after_create
method of your observer is invoked before paperclip'safter_save
method is run which processes the attachment.Consider using
after_commit
orar_after_transaction
instead ofafter_save
etc. When communicating with an external API this is what you want anyway, since otherwise API calls can not be undone if the transaction is rolled back.我不知道你的观察者和代码的内部结构。一个可能的解决方案是更改在
after_create
事件上触发的观察者。I don't know the internals of your observer and your code. A possible solution would be to change the observer to be triggered on the
after_create
event.