Rails活动记录复制和保存失败

Rails active record duplication and save failure

我有这个错误:Errno::ENOENT in PicturethingsController#update_profile

No such file or directory - /Users/Baz/rails/myapp/public/uploads/picturething/picture/49/cat.jpg

我正在尝试复制标准图片记录并将其保存到@character.profilepicture。我也无法将@character.profilepicture 保存到数据库(我从进入rails 控制台后检查知道)。

这是违规的方法:

picturethings_controller.rb:

def update_profile
  @character = Character.find_by(callsign: params[:callsign])
  standardpicture = Picturething.find_by(id: params[:picid])
  @character.build_profilepicture
  @character.profilepicture.save!
  @character.profilepicture = standardpicture.dup
  @character.profilepicture.save!
  @character.profilepicture.picture.recreate_versions!
  @character.profilepicture.picture = @character.profilepicture.picture.profile
  respond_to do |format|
    format.html do
      redirect_to @character.sociable
    end
    format.js
  end
end

character.rb:

has_many :standardpictures, class_name: "Picturething",
                            inverse_of: :character,
                            foreign_key: "character_standard_id",
                            dependent: :destroy
has_one  :profilepicture,   class_name: "Picturething",
                            inverse_of: :character,
                            foreign_key: "character_profile_id",
                            dependent: :destroy

picturething.rb:

mount_uploader :picture, CharacterpicUploader
@character.profilepicture = standardpicture.dup

问题在于它只是复制了 ActiveRecord 对象的属性,而不会复制磁盘上的实际图片,所以当您这样做时

@character.profilepicture.picture = @character.profilepicture.picture.profile

它抛出 No such file or directory 错误。尝试使用 this gem 来帮助您在 AR 记录之间复制 CarrierWave 附件。