我有两个模型,每个模型都有自己的载波上传器:

class User < ActiveRecord::Base
  mount_uploader :avatar, AvatarUploader
end


和:

class Bookshelf < ActiveRecord::Base
  mount_uploader :image, ImageUploader
end


我希望用户的头像是他上传的最新书架图片。我试图做到这一点是这样的:

class BookcasesController < ApplicationController
  def create
    @bookcase = current_user.bookcases.build(params[:bookcase])
    if @bookcase.save
      current_user.avatar = @bookcase.image
      current_user.avatar.recreate_versions!
    end
  end
end


不幸的是,这完全不会影响头像。我还能怎么实现呢?

最佳答案

current_user.avatar = @bookcase.image
current_user.avatar.recreate_versions!


实际上并不保存---您可以:

current_user.avatar.save


或如您所说:

current_user.update_attribute(:avatar, @bookcase.image)

07-27 17:36