Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paperclip attachment not using proper updated_at time for query string

I'm using Rails + Paperclip + S3. I'm looking to host my assets to Cloudfront using one of my S3 buckets.

I know that Cloudfront caches assets but that you can break that cache by configuring it to forward query strings from the origin.

When I modify an asset I expect this to happen

  1. The updated_at timestamp of the ActiveRecord object is updated.
  2. Paperclip updates the query string of the image on S3 to reflect the new updated_at time.
  3. Cloudfront serves the new image because of the new timestamp query string.

However, it looks like Paperclip isn't properly updating the updated_at timestamp. Maybe it's cached somehow. Therefore the query string doesn't update and Cloudfront never breaks the cache.

My model is:

class UserImage < ActiveRecord::Base
  has_attached_file :image
end

>> ui = UserImage.find(576925)
>> ui.image.class
=> Paperclip::Attachment

>> ui.touch

>> ui.updated_at.to_i
=> 1386241041

>> ui.image.updated_at
=> 1386240937

Those two updated_at timestamps should be the same.

like image 223
johnnymire Avatar asked Dec 06 '13 16:12

johnnymire


1 Answers

It looks like this might have been a bug with earlier versions of Paperclip (I'm on 2.7) as it works on future versions.

For reference this is how I got it working. In the after_update on my model I force refreshed the instance as follows:

image.instance_write(:updated_at, Time.now.utc)
self.send(:update_without_callbacks)

I had to do update_without_callbacks so that it wouldn't get stuck in an infinite loop in the callback.

Now the updated_at time on the Attachment is updated, forcing refresh on Cloudfront.

like image 83
johnnymire Avatar answered Nov 17 '22 02:11

johnnymire