views:

335

answers:

1

Hi, in Rails using the Polymorphic version of Paperclip, the default saving technique means that files with the same name overwrite each other. Including the :id in the path and URL doesn't work as it just overwrites the earlier file with the old :id.

I've tried interpolations using a time-stamp, but it just looks for the current time when being shown the image (plus as I have multiple thumbnails it takes longer than a second, therefore the images have different stamps).

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

Also tried using a hex number, but it iterates through for each thumb, thus breaking as there's a new hex value each time.

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

As it's the Polymorphic version, and thus has it's own model, I don't know how to access the values from the parent model (in this case "Post"). Variations on the code below all throw up "undefined method" errors.

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

Sorry if it seems a newbie question, but it's well document for the traditional Paperclip, but nothing is out there for the Polymorphic fork.

A: 

I think you are doing something wrong, either in your model, or your attachment naming scheme. Because, by default, it's not possible for this to happen.

WHat are your namings?

Terry
Namings for what? I have assets and attachings tables in my database, which are the standard PaperclipPolymorph ones.No matter what I put in the URL, and no matter whether it includes the time/id of the asset, it just gets overwritten when a file of the same name is uploaded.
Steve F