Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 carriwave,克隆对象如何包含图像_Ruby On Rails 3_Ruby On Rails 3.1_Ruby On Rails 3.2_Gem - Fatal编程技术网

Ruby on rails 3 carriwave,克隆对象如何包含图像

Ruby on rails 3 carriwave,克隆对象如何包含图像,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,gem,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,Gem,第一次我使用的是db mysql,但后来我改为postgres 我写了一些函数将数据库从sql克隆到postgres。我在从映像表克隆数据库时遇到问题 ImageClone.all.each do |p| img = Image.new() img.imageable_id = p.imageable_id img.imageable_type = p.imageable_type img.uploader = p.uploader puts p.uplo

第一次我使用的是db mysql,但后来我改为postgres

我写了一些函数将数据库从sql克隆到postgres。我在从映像表克隆数据库时遇到问题

ImageClone.all.each do |p|
    img = Image.new()
    img.imageable_id = p.imageable_id
    img.imageable_type = p.imageable_type
    img.uploader = p.uploader
    puts p.uploader #=> 'testing_image.jpg'
    img.save
end



=> #<Image id: 1, imageable_id: 1, imageable_type: "ProductType", uploader: nil> 
ImageClone.all.each do|p|
img=Image.new()
img.imageable\u id=p.imageable\u id
img.imageable_type=p.imageable_type
img.uploader=p.uploader
将p.uploader#=>“testing_image.jpg”
img.save
结束
=> # 
如何设置图像url='testing_image.jpg'。
有什么建议吗?

如果您想将附件复制到新对象中,您需要告诉新对象阅读原始附件,如下所示:

new_object.attachment_file = File.open(old_object.attachment_file.path)
其中,
attachment\u file
是定义为附件类中装载的上载程序的属性:

class Object < ActiveRecord::Base
  mount_uploader :attachment_file, AttachmentUploader
class对象
现在,您的新对象已将文件加载到内存中。您可以像其他附件一样保存、验证或放弃它