简体   繁体   English

与Sidekiq的回形针不起作用

[英]Paperclip with Sidekiq not working

I have created a Sidekiq worker that will duplicate an existing record with Paperclip attachment, but it doesn't seem to be working. 我创建了一个Sidekiq工作程序,该工作程序将复制具有Paperclip附件的现有记录,但似乎无法正常工作。

#controller
product = Product.find(2)
SuperWorker.perform_in(5.minutes, product.id)

#worker
class SuperWorker
  include Sidekiq::Worker
  def perform(product_id)
    product = Product.find(product_id)
    product.generate_clone
  end
end

#product model
...
has_attached_file :front_image, :styles => { :medium => "415x500>", :thumb => "100x100>" }, :default_url => "/assets/thumbnail-default.jpg"
validates_attachment_content_type :front_image, :content_type => ['image/png']

has_attached_file :back_image, :styles => { :medium => "415x500>", :thumb => "100x100>" }, :default_url => "/assets/thumbnail-default.jpg"
validates_attachment_content_type :back_image, :content_type => ['image/png']

def generate_clone
  new_product = self.dup
  new_product.front_image = self.front_image
  new_product.back_image = self.back_image
  new_product.save
end

When I do the duplicating of record in the console, it seems to work that is why I am very puzzled why it is not working in the scheduled task. 当我在控制台中复制记录时,似乎可以正常工作,这就是为什么我很困惑为什么它不能在计划任务中工作的原因。 Here is how I did it in rails console. 这是我在Rails控制台中做到的方法。

p = Product.find(2)
new_p = p.dup
new_p.front_image = p.front_image
new_p.back_image = p.back_image
new_p.save

This works fine, but in the sidekiq worker it does not. 这可以正常工作,但在sidekiq worker中则不能。

I hope you can shed some light on whatever I did wrong with this one and/or if I missed something out. 希望您能阐明我在此操作上做错的任何事情和/或如果我错过了某些事情。

Thank you. 谢谢。

Eralph 拉尔夫

I resolved this by not using the .dup function 我通过不使用.dup函数解决了此问题

p = Product.find(2)
new_p = Product.new
new_p.field1 = p.field1
new_p.field2 = p.field2
...
new_p.front_image = p.front_image
new_p.back_image = p.back_image
new_p.save

Works perfectly fine. 工作完美。 I hope this can help anyone who's having a problem with this. 我希望这可以帮助任何对此有疑问的人。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM