Artur79
Artur79

Reputation: 13667

paperclip, record is saved before a file saved on disk

I'm uploading an image and there is observer which is triggered when new record is created. In observer I'm pushing image via API to other service. Problem is when doing it locally or in rspec test, seems that record is saved in DB before image is saved on disk and I'm getting file not found error. How to make it to keep correct order ?

Upvotes: 1

Views: 466

Answers (2)

tfischbach
tfischbach

Reputation: 3051

Observers are triggered before callbacks defined in the model. Therefore the after_create method of your observer is invoked before paperclip's after_save method is run which processes the attachment.

Consider using after_commit or ar_after_transaction instead of after_save etc. When communicating with an external API this is what you want anyway, since otherwise API calls can not be undone if the transaction is rolled back.

Upvotes: 2

Simone Carletti
Simone Carletti

Reputation: 176472

I don't know the internals of your observer and your code. A possible solution would be to change the observer to be triggered on the after_create event.

Upvotes: 0

Related Questions