【问题标题】:Attachments adding duplicates in active storage附件在活动存储中添加重复项
【发布时间】:2021-12-23 01:47:13
【问题描述】:

我有一个消息模型

 has_many_attached :attachments

我的功能:

  def update_attachment
        remaining_attachment_ids = attachment_params[:existing]
        if message.attachments.attached? && errors.empty?
          message.attachments.where.not(id: remaining_attachment_ids).purge
          message.reload
          puts "AFTER DELETE"
          puts message.attachments.count
        end
        message.reload

        attach_new_files
      end

      def attach_new_files
        if attachment_params[:new]
          puts attachment_params[:new].map { |attachment| attachment["blob_signed_id"]}
          message.attachments.attach(attachment_params[:new].map { |attachment| attachment["blob_signed_id"] })
          message.reload
          puts "AFTER UPDATE"
          puts message.attachments.count
        end
      end

我正在尝试清除一些附件,然后分两步添加新附件。首先,我清除了附件,message.attachments.count 打印正确。

但是,当我执行 attach_new_files 时,在附件之后,我得到了额外的计数。例如,我在 attachment_params[:new] 数组中只有 2 项。当我在附加后打印计数时,它显示 3。我只提供了 2 个 blob_signed_ids 来附加。这可能是什么问题?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 rails-activestorage


    【解决方案1】:

    对我有用的是为活动存储设置配置:

    config.active_storage.replace_on_assign_to_many = true
    

    之前设置为 false。

    【讨论】:

      猜你喜欢
      • 2019-05-28
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 2020-11-04
      • 2019-01-24
      • 2022-10-21
      相关资源
      最近更新 更多