复制带有关联和回形针附件的导轨对象 [英] Duplicate a rails object with associations and paperclip attachments

查看:54
本文介绍了复制带有关联和回形针附件的导轨对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个关联的对象.其中一些关联对象具有存储在 S3 中的回形针附件.如果我复制对象和关联,它可以正常工作,但附件不会重复.

I have an object with several associations. Some of these associated objects have paperclip-attachments stored at S3. If I duplicate the object and the associations it works fine but the attachments are not duplicated.

此处无需获取图像即可工作:

This here works without getting the images:

copy_salon = @salon.dup 
copy_salon.about_us_versions = @salon.about_us_versions.collect{|about_us| about_us.dup}

我尝试像这样获取图片链接:

I tried to get the image link like this:

copy_salon = @salon.dup 
copy_salon.about_us_versions = @salon.about_us_versions.collect{|about_us| 
                                                                  about_us_dup = about_us.dup
                                                                  if about_us.about_us_image then about_us_dup.about_us_image = about_us.about_us_image end
                                                                  if about_us.team_image then about_us_dup.team_image = about_us.team_image end
                                                                  about_us_dup
                                                                }

但后来我收到错误消息无法将 nil 转换为字符串",可能是因为并非所有图像都已设置.

But then I am getting the error 'can't convert nil into String', probably because not all images are set.

推荐答案

明白了,不优雅但有效.我曾希望 dup 能用所有关联和附件复制我的对象.难道没有任何宝石吗?

Got it, not elegant but working. I had hoped dup would duplicate my object with ALL associations and attachments. Isn't there any gem for that?

copy_salon = @salon.dup 
copy_salon.about_us_versions = @salon.about_us_versions.collect{|about_us| 
                                                                  about_us_dup = about_us.dup
                                                                  unless about_us.about_us_image.url == "/about_us_images/original/missing.png" then about_us_dup.about_us_image = about_us.about_us_image end
                                                                  unless about_us.team_image.url == "/team_images/original/missing.png" then about_us_dup.team_image = about_us.team_image end
                                                                  about_us_dup
                                                                }

这篇关于复制带有关联和回形针附件的导轨对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆