【发布时间】:2013-08-31 01:41:37
【问题描述】:
Thoughtbot 的 Paperclip gem (v 3.2.1) 将清理文件名,用下划线替换空格和其他特殊字符。我正在重新导入数据,需要检查附件是否已经上传,但原始文件名可能与回形针中的附件名不匹配。回形针用什么方法清理?
【问题讨论】:
Thoughtbot 的 Paperclip gem (v 3.2.1) 将清理文件名,用下划线替换空格和其他特殊字符。我正在重新导入数据,需要检查附件是否已经上传,但原始文件名可能与回形针中的附件名不匹配。回形针用什么方法清理?
【问题讨论】:
找到深入研究源代码的答案。这是一个私有实例方法 Paperclip::Attachment#cleanup_filename 。 (由于这是一个短暂的(一次性)导入任务,我不介意使用未发布方法的(轻微)风险。)
因此我的代码看起来像这样(Post has_many :attachments; Attachment has_attached_file :attached)
if @post.attachments.present?
cleaned_filename = @post.attachments.first.attached.send :cleanup_filename, filename
if @post.attachments.map(&:attached_file_name).include? cleaned_filename
puts "already attached: #{filename}"
return
end
end
puts "attaching upload: #{filename}"
【讨论】:
cleaned_filename = post.plan_pdf.send(:cleanup_filename, plan_pdf_name) 其中 post 是新记录或在数据库中找到的记录。谢谢! (这不是回答原始问题,只是找到了一种通过您调用 cleanup_filename 的干净方法)