Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails active record duplication and save failure

I have this error: Errno::ENOENT in PicturethingsController#update_profile.

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

I'm trying to duplicate the standardpicture record and save that to @character.profilepicture. I also can't get @character.profilepicture to save to the database (I know from checking after entering the rails console).

Here is the offending method:

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
like image 214
Bazley Avatar asked Jul 13 '15 15:07

Bazley


1 Answers

@character.profilepicture = standardpicture.dup

The problem with this is that it's only copying the attributes of the ActiveRecord object, but it won't copy the actual picture on the disk, so when you then do

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

it throws a No such file or directory error. Try using this gem to help you copy your CarrierWave attachments between AR records.

like image 110
Adib Saad Avatar answered Oct 28 '22 22:10

Adib Saad