13

我最近将我的项目升级到最新的 Rails 版本(5.2)以获得ActiveStorage- 一个处理附件上传到 AWS S3、Google Cloud 等云服务的库。

几乎一切正常。我可以上传和附加图片

user.avatar.attach(params[:file])

并收到它

user.avatar.service_url

但现在我想替换/更新用户的头像。我以为我可以跑

user.avatar.attach(params[:file])

再次。但这会引发错误:

ActiveRecord::RecordNotSaved: Failed to remove the existing associated avatar_attachment. The record failed to save after its foreign key was set to nil.

那应该是什么意思?如何更改用户头像?

4

4 回答 4

19

错误的原因

has_one此错误是由您的模型和附件记录之间的关联引发的。发生这种情况是因为尝试用新的附件替换原始附件会孤立原始附件并导致它无法通过belongs_to关联的外键约束。这是所有 ActiveRecordhas_one关系的行为(即它不是特定于 ActiveStorage)。

一个类似的例子

class User < ActiveRecord::Base
   has_one :profile
end
class Profile < ActiveRecord::Base
   belongs_to :user
end

# create a new user record
user = User.create!

# create a new associated profile record (has_one)
original_profile = user.create_profile!

# attempt to replace the original profile with a new one
user.create_profile! 
 => ActiveRecord::RecordNotSaved: Failed to remove the existing associated profile. The record failed to save after its foreign key was set to nil.

在尝试创建新配置文件时,ActiveRecord 尝试将user_id原始配置文件的 设置为nil,这使记录的外键约束失败belongs_to。我相信这本质上是当您尝试使用 ActiveStorage 将新文件附加到模型时发生的事情......这样做会尝试使原始附件记录的外键无效,这将失败。

解决方案

关系的解决方案has_one是在尝试创建新记录之前销毁相关记录(即在尝试附加另一个记录之前清除附件)。

user.avatar.purge # or user.avatar.purge_later
user.avatar.attach(params[:file])

这是期望的行为吗?

在尝试为 has_one 关系附加新记录时,ActiveStorage 是否应该自动清除原始记录是一个不同的问题,最好向核心团队提出......

IMO 让它与所有其他 has_one 关系保持一致是有道理的,最好让开发人员在附加新记录之前明确清除原始记录而不是自动执行(这可能有点冒昧) )。

资源:

于 2017-08-29T14:07:14.677 回答
4

您可以在使用purge_later前调用:attachhas_one_attached

user.avatar.purge_later
user.avatar.attach(params[:file])

更新

Rails 现在会自动清除以前的附件(自 8 月 29 日起)

于 2017-08-29T07:59:22.357 回答
1

我在保存图像时遇到了同样的问题。我希望这个能帮上忙

class User < ApplicationRecord
  has_one_attached :avatar
end

让我们看看表单和控制器

= simple_form_for(@user) do |f|
  = f.error_notification
  .form-inputs
    = f.input :name
    = f.input :email
    = f.input :avatar, as: :file

  .form-actions
    = f.button :submit

控制器/posts_controller.rb

def create
    @user = User.new(post_params)
    @user.avatar.attach(params[:post][:avatar])
    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
于 2018-02-02T10:12:08.207 回答
0

如果您使用嵌套属性并且子模型中没有其他属性发生更改,Rails 不会自动检测附件的更改。为此,您必须重写该changed_for_autosave?方法:

def Child
  belongs_to :parent
  has_one_attached :attachment

  # Magic happens here
  def changed_for_autosave?
    super || attachment.changed_for_autosave?
  end
end

def Parent
  has_many :children

  accepts_nested_attributes_for :children
end

这也会在父保存时触发子模型回调 ( before_save, ...)。我不知道这种方法在没有嵌套属性的情况下是否有效,但我想它可以。一般来说,这种逻辑不应该像许多人建议的那样在控制器内部处理(在我看来)。

我花了一段时间才弄清楚,我希望这会有所帮助。干杯!

于 2020-05-04T15:31:59.740 回答