小编Ism*_*urg的帖子

Rails:将Tempfile传递给Sidekiq

我在一个邮件端点上工作,将附件收集为Tempfile,然后我需要将它们传递给Sidekiq Worker以将它们上传到AWS.

我的问题是,我一直在试图坚持Tempfile,然后在我的工作人员中打开它.我不知道我应该打开我的Tempfile(路径,文件名......).

这是我的函数,它将调用worker:

if @email
    # Remove Tempfile autodelete
    @email.attachments.each {|t| ObjectSpace.undefine_finalizer(t.tempfile)}

    # Griddler Email to hash for Sidekiq
    email = {
        attachments: @email.attachments.map {|att| {
            type: att.content_type,
            name: att.original_filename
        }},
        raw_text: @email.raw_text,
        raw_html: @email.raw_html,
        from: @email.from,
        subject: @email.subject,
        to: @email.to,
        cc: @email.cc
    }

    EmailResponseWorker.perform_async email
end
Run Code Online (Sandbox Code Playgroud)

这里我ObjectSpace.undefine_finalizer(t.tempfile)用来禁用自动删除.

然后在我的Sidekiq工人:

def perform(email)
  @email = email

  attachments = @email['attachments'].inject([]) do |arr, file|
    object = S3_BUCKET.objects["attachments/#{SecureRandom.uuid}/#{file['name']}"].write(Tempfile.open(file['name']), acl: :public_read)
    arr << {url: object.public_url.to_s, type: file['type'], name: file['name']}
  end
end
Run Code Online (Sandbox Code Playgroud)

这里附件['name']是文件的名称.

ruby-on-rails sidekiq

5
推荐指数
1
解决办法
835
查看次数

Rails模型 - 从未调用after_destroy

我在模型中使用after_destroy有些麻烦

这是这一个:

class Transaction < ActiveRecord::Base
  belongs_to :user
  delegate :first_name, :last_name, :email, to: :user, prefix: true

  belongs_to :project
  delegate :name, :thanking_msg, to: :project, prefix: true

  validates_presence_of :project_id

  after_save :update_collected_amount_in_project
  after_update :update_collected_amount_if_disclaimer
  after_destroy :update_collected_amount_after_destroy

  def currency_symbol
    currency = Rails.application.config.supported_currencies.fetch(self.currency)
    currency[:symbol]
  end

  private

  def update_collected_amount
    new_collected_amount = project.transactions.where(success: true, transaction_type: 'invest').sum(:amount)
    project.update_attributes(collected_amount: (new_collected_amount / 100).to_f) # Stored in € not cents inside projects table
  end

  def update_collected_amount_in_project
    update_collected_amount if transaction_type == 'invest' && success == true
  end

  def update_collected_amount_if_disclaimer
    update_collected_amount if transaction_type …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails ruby-on-rails-4

4
推荐指数
1
解决办法
1112
查看次数