这取决于您对附件建模的方式(例如 s3 的路径和 url 方案),但在最常见的情况下,您可以简单地创建具有必要属性(包括文件名)的对象,这些属性应该从 upload_url 值派生而来。然后可以访问上传记录,就好像它最初是通过回形针创建的一样。
1.9.3p194 :069 > u = Attachment.new
=> #<Attachment id: nil, type: nil, attachable_id: nil, attachable_type: nil, attachment_file_name: nil, attachment_content_type: nil, attachment_file_size: nil, attachment_updated_at: nil, created_at: nil, updated_at: nil>
1.9.3p194 :070 > u.attachment_file_name = 'fdssfd.txt'
=> "fdssfd.txt"
1.9.3p194 :071 > u.attachment
=> /system/assets/images/paperclip/development//original/fdssfd.txt
1.9.3p194 :072 > u.save
(0.3ms) BEGIN
SQL (0.5ms) INSERT INTO `attachments` (`attachable_id`, `attachable_type`, `attachment_content_type`, `attachment_file_name`, `attachment_file_size`, `attachment_updated_at`, `created_at`, `type`, `updated_at`) VALUES (NULL, NULL, NULL, 'fdssfd.txt', NULL, NULL, '2012-09-18 14:24:26', NULL, '2012-09-18 14:24:26')
[paperclip] Saving attachments.
(1.4ms) COMMIT
=> true
1.9.3p194 :073 > u.attachment
=> /system/assets/images/paperclip/development/5/original/fdssfd.txt