如何更新 ActiveStorage 中的附件 (Rails 5.2)
How to update attachment in ActiveStorage (Rails 5.2)
我最近将我的项目升级到最新的 Rails 版本 (5.2) 以获得 ActiveStorage
- 一个处理附件上传到 AWS S3、Google Cloud 等云服务的库..
几乎一切正常。我可以使用
上传和附加图片
user.avatar.attach(params[:file])
并通过
接收
user.avatar.service_url
但是现在我想replace/update一个用户的头像。我以为我可以运行
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.
这是什么意思?如何更改用户头像?
使用has_one_attached
时可以在attach
之前调用purge_later
:
user.avatar.purge_later
user.avatar.attach(params[:file])
更新
Rails now purges previous attachment automatically (since Aug 29th).
错误原因
此错误是由您的模型和附件记录之间的 has_one
关联引起的。发生这种情况是因为尝试用新附件替换原始附件将使原始附件孤立并导致它无法满足 belongs_to
关联的外键约束。这是所有 ActiveRecord has_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 关系一致工作是有道理的,最好让开发人员在附加新记录之前明确清除原始记录而不是做它会自动(可能有点冒昧)。
资源:
我在保存图片时遇到了同样的问题。我希望这会有所帮助
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
controllers/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
如果您正在使用嵌套属性并且子模型中没有其他属性发生变化,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
,...)。
我不知道这种方法在没有嵌套属性的情况下是否有效,但我想它可以。
一般来说,这种逻辑不应该像许多人建议的那样在控制器内部处理(在我看来)。
我花了一段时间才弄清楚,希望这对您有所帮助。干杯!
我最近将我的项目升级到最新的 Rails 版本 (5.2) 以获得 ActiveStorage
- 一个处理附件上传到 AWS S3、Google Cloud 等云服务的库..
几乎一切正常。我可以使用
上传和附加图片user.avatar.attach(params[:file])
并通过
接收user.avatar.service_url
但是现在我想replace/update一个用户的头像。我以为我可以运行
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.
这是什么意思?如何更改用户头像?
使用has_one_attached
时可以在attach
之前调用purge_later
:
user.avatar.purge_later
user.avatar.attach(params[:file])
更新
Rails now purges previous attachment automatically (since Aug 29th).
错误原因
此错误是由您的模型和附件记录之间的 has_one
关联引起的。发生这种情况是因为尝试用新附件替换原始附件将使原始附件孤立并导致它无法满足 belongs_to
关联的外键约束。这是所有 ActiveRecord has_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 关系一致工作是有道理的,最好让开发人员在附加新记录之前明确清除原始记录而不是做它会自动(可能有点冒昧)。
资源:
我在保存图片时遇到了同样的问题。我希望这会有所帮助
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
controllers/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
如果您正在使用嵌套属性并且子模型中没有其他属性发生变化,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
,...)。
我不知道这种方法在没有嵌套属性的情况下是否有效,但我想它可以。
一般来说,这种逻辑不应该像许多人建议的那样在控制器内部处理(在我看来)。
我花了一段时间才弄清楚,希望这对您有所帮助。干杯!