Duplicate a rails object with associations and paperclip attachments

你。 提交于 2019-12-12 18:07:26

问题


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
                                                                }

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


回答1:


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
                                                                }



回答2:


I got it simpler by overriding dup, at least for Paperclip attachments:

def dup
  duplicate = super

  # attachment_definitions is defined if model has paperclip attachments
  return duplicate unless self.class.respond_to?(:attachment_definitions)

  duplicate.tap do |d|
    self.class.attachment_definitions.keys.each do |name|
      d.send("#{name}=", send(name)) if send(name).exists?
    end
  end
end

It can be defined like this in ApplicationRecord so every model benefits from it.



来源:https://stackoverflow.com/questions/11443148/duplicate-a-rails-object-with-associations-and-paperclip-attachments

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