Polymorphic paper clips rewriting files with the same name

in Rails using the Polymorphic version of Paperclip, the default save method means that files with the same name overwrite each other. Including the identifier: id in the path and the URL does not work, as it just overwrites the previous file with the old: id.

I tried interpolation using a timestamp, but it just looks for the current time when displaying the image (plus, since I have several thumbnails, it takes more than a second, so the images have different brands).

Paperclip.interpolates :uniqueid do |attachment, style|
  Time.now.to_i.to_s
end

Also tried using a hexadecimal number, but it iterates over each thumb, thus breaking up like a new hexadecimal value appears each time.

Paperclip.interpolates :uniqueid do |attachment, style|
  ActiveSupport::SecureRandom.hex(3)
end

As in the Polymorphic version and, therefore, has its own model, I do not know how to access the values ​​from the parent model (in this case, "Mail"). The code variants below all cause "undefined" errors.

Paperclip.interpolates :user_id do |attachment, style|
  current_user.id
end

Sorry if this is a newbie question, but it documents the traditional Paperclip well, but nothing exists for a polymorphic fork.

+3
source share
1 answer

How about turning on for ? :class:pathhas_attached_file

has_attached_file :attachment,
  :path => ":rails_root/attachments/:class/:id/:attachment/:basename.:extension",
  :url => "downloads/:id/:title.:extension"
0
source

Source: https://habr.com/ru/post/1736100/


All Articles