Paperclip - Running a method after the file is saved?
Asked Answered
S

4

10

I'm working on a project that needs to accept file uploads. After the file is uploaded, I'm doing some processing - extracting information from the file. I eventually plan to run this in a background worker, but it's currently running inline.

I've tried making use of both after_create and after_save to process the file, but it seems my method is ran before the save method from Paperclip - so my tests fail with "No such file or directory".

Is there any way to trigger the save method early, or to somehow run my method after the file has been saved to the file system?

Schlimazel answered 13/11, 2013 at 17:31 Comment(0)
U
7

You can't read paperclip file in a callback as it's not saved to filesystem yet (or butt). Why, I'm not exactly sure.

EDIT: Reason is that paperclip writes out the file via after_save callback. That callback happens after after_create

However you can get the file payload for your processing. For example:

class Foo < ActiveRecord::Base

  has_attached_file :csv

  after_create :process_csv

  def process_csv
    CSV.parse(self.csv.queued_for_write[:original].read)
    # .. do stuff
  end

end

I had to do this 2 minutes ago. Hope this helps.

Unfetter answered 29/1, 2015 at 21:34 Comment(4)
How can i do for image? When upload image using paperclip then want to send email after create record but no such file error get. I want to send email with attachment of that image. What i have to do?Vuong
I have the same problem @user1780370. Did you find a solution?Mendoza
I did not do after that because some requirements changes.Vuong
as @jonmichael-chambers answered in the comment below after_commit works pretty great in this caseInfallibilism
A
3

Adding this answer for visibility. A previous comment by @Jonmichael Chambers in this thread solved the problem for me.

Change the callback from after_save/after_create to after_commit

Archdeaconry answered 30/12, 2015 at 6:9 Comment(0)
I
2

I think the problem might be related to the order of callbacks.

As discussed in other answers, the attachment file is indeed physically saved to disk in an after_save callback defined in Paperclip which is added to the model class at the point of has_attached_file call.

So you must ensure that your own after_save callbacks (that want to deal with the uploaded file) are defined after the has_attached_line.

Note: the after_create callback indeed cannot be used at all as it is called before after_save.

Isolecithal answered 9/3, 2016 at 10:41 Comment(0)
W
0

Take a look at the Paperclip post processing events callbacks. You should be able to call after_post_process to do your extra file info extraction.

Wisconsin answered 13/11, 2013 at 18:40 Comment(5)
Thanks for the response. I have tried after_post_process, but I still have the same issue. "No such file or directory". If I remove all of the validation completely, I can see the file is being saved at some point - but perhaps not at the time of calling after_post_process. edit: Scratch that, I seem to have figured it out. Thanks!Schlimazel
Ugh, nevermind. I was mistaken. When I use a method that expects the File to be saved already, it's still throwing "not found" errors. :|Schlimazel
I found I was able to run my method from after_commit.. but is that really the best way to do it? It seems the file is saved before that point.Schlimazel
@JonmichaelChambers thanks for comment. after_commit worked for me as well.Protector
The after_commit does not work for me. I still have the same error. Do you know any other solution?Mendoza

© 2022 - 2024 — McMap. All rights reserved.