Search code examples
ruby-on-railspaperclipsidekiq

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.

#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.

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.

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


Solution

  • I resolved this by not using the .dup function

    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.