我想在上传文件之前计算一个哈希值,这样就不会在服务器上存储重复项。
使用paperclip gem,在保存文件或在数据库中插入数据之前,对文件进行处理的最佳方法是什么?
答案 0 :(得分:2)
ActiveModel有一个回调before_create
(以及其他),这将成为您在创建记录之前计算内容的理想场所。有关可用回调的完整列表,请参阅Ruby on Rails Guides: Active Record Validations and Callbacks。
class Asset
has_attached_file :image
before_create :do_something
def do_something
end
end