覆盖 Paperclip:Attachment 方法

Override Paperclip:Attachment method

我需要在上传附件时从附件访问 url,因此我必须定义 after_flush_writes 方法,就像这里建议的那样 https://github.com/thoughtbot/paperclip/issues/816

我怎样才能做到这一点?

到目前为止我已经这样做了,但它不起作用:

class ExcelFile < ActiveRecord::Base
  belongs_to :insertion 
  has_attached_file :excel
  validates_attachment_content_type :excel, content_type: [ "application/vnd.ms-excel",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "application/msword",
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]


  def after_flush_writes
    super
    byebug
    file = Roo::Excelx.new(self.excel.url(:original, timestamp: false))
    input_from_generals self.id, file
    input_from_financials self.id, file
  end

我尝试使用 after_commitafter_create 回调来访问 self.excel.url,但它不起作用。

我认为问题可能与回调的顺序有关。

正如上面评论中所讨论的,附件文件确实以 after_save callback defined in Paperclip 的形式物理保存到磁盘,并在 [=] 点添加到模型 class 11=] 呼叫.

所以你必须确保你自己的 after_save 回调(想要处理上传的文件)是在 has_attached_line 之后定义的。

注意:after_create 回调确实根本不能使用,因为它在 after_save.

之前被调用