Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave: Duplicating File In a Second Model

I have two models, each with their own Carrierwave uploaders:

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

and:

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

I want the user's avatar to be the latest bookshelf image he's uploaded. I try to achieve this like so:

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

Unfortunately, this has no effect on the avatar at all. How else might I achieve this?

like image 396
nullnullnull Avatar asked Dec 27 '22 20:12

nullnullnull


1 Answers

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

Doesn't actually save --- you can either:

current_user.avatar.save

or as you put:

current_user.update_attribute(:avatar, @bookcase.image)
like image 72
Jesse Wolgamott Avatar answered Jan 10 '23 01:01

Jesse Wolgamott