How to reference parent model from Carrierwave Uploader object

梦想的初衷 提交于 2019-12-10 16:55:52

问题


I want to run some methods after the upload of a file has been completed via Carrierwave. Carrierwave includes several callback methods detailed here.

I'm planning on using the :store callback.

That's great, but it requires me to run the callback method in the Uploader object instance. This isn't the end of the world, but I want to keep all the logic inside of my parent Model.

My question is: How should I be referencing the parent model from the Uploader? I don't see anything obvious in the documentation?

I suppose I could do something like

ParentModel.last.call_my_method_here

but this seems like a very brittle way to code this (not expecting a lot of requests, but still).


回答1:


I guess the model variable is what you were looking for, see examples using it here




回答2:


For what it's worth, I have used the after_commit callback on parent model which so far seems to be working properly. This is allowing me to interact with the uploaded file as well.



来源:https://stackoverflow.com/questions/13060784/how-to-reference-parent-model-from-carrierwave-uploader-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!