首页 > 解决方案 > 更改 CarrieWave 模型的文件名

问题描述

我有一个这样的 Rails 模型,使用 ImageUploader 继承CarrierWave::Uploader::Base

class Image < ApplicationRecord
  mount_uploader :file, ImageUploader
end

我的应用程序中的一个错误导致images.file列值与 GCS 中的实际文件名不匹配,所以我想这样做:

Image.first.file = "fixed_file_name.jpg"
Image.save

Image#file不返回其file列值,而是返回CarrierWave::Uploader::Base实例。

如何更新文件名,就像UPDATE ... SET ...在 SQL 中一样?

标签: ruby-on-railscarrierwave

解决方案


大概是这样的:User.where(id: 1).update_all(avatar: 'x.png')


推荐阅读