Rails、Paperclip、S3、未定义的方法'size'

Rails, Paperclip, S3, undefined method 'size'

我正在使用

ruby 2.2.0
paperclip (4.3.2)
aws-sdk (1.66.0)

尝试将文件上传到 AWS S3 时出现以下错误

NoMethodError - undefined method `size' for nil:NilClass:
  paperclip (4.3.2) lib/paperclip/attachment.rb:466:in `reset_file_if_original_reprocessed'
  paperclip (4.3.2) lib/paperclip/attachment.rb:110:in `assign'
  paperclip (4.3.2) lib/paperclip/has_attached_file.rb:66:in `block in define_setter'
  activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
  activerecord (4.2.0) lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
  actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
  activerecord (4.2.0) lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
  activerecord (4.2.0) lib/active_record/persistence.rb:249:in `block in update'
  activerecord (4.2.0) lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
  activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
  activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
  activerecord (4.2.0) lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
  activerecord (4.2.0) lib/active_record/persistence.rb:248:in `update'

这个错误对我来说似乎很奇怪,因为上传到 S3 有效并且文件存储在那里:

[paperclip] saving /users/2/identity_documents/xxx.PDF
[AWS S3 200 0.768066 0 retries] put_object(:acl=>:private,:bucket_name=>"imp-staging",:content_length=>167130,:content_type=>"application/pdf",:data=>Paperclip::UploadedFileAdapter: xxx.PDF,:key=>"users/2/identity_documents/xxx.PDF")  

我也注意到,class Attachment 中的 @queued_for_write 是空的。

有人观察到相同的行为吗?

在筛选了所有相关的 Paperclip 问题后,我自己解决了这个问题。

我在上传文档后使用 after_*_post_process 回调来更新我的模型。似乎在尝试 save 此回调中的模型时出现问题,所以我切换到 update_column 并且一切都按预期工作。