0

I want migrate my images from one model to another. My old model looks like:

class Post < ActiveRecord::Base

     has_attached_file :logo,
                  url: '/test/post/logo',
                  path: ':rails_root/uploads/test/post/:id/logo/:hash',
                  hash_secret: 'secret',
                  styles: { thumb: ['200x150>', :jpg], medium: ['320x240>', :jpg], large: ['480x360>', :jpg] }
end

the new model looks like the old one.

Currently i try to migrate the images with the following code snippet:

new_logo = post.logo
new_image.logo = new_logo
new_image.save

But unfortunately is doesn't work. I get the following error:

No such file or directory @ rb_sysopen - /abcde/fghjk/test/post/1/logo/43023e427c1deb69789bbf7b75cf32810fbb6354

When i search for the hash in the directory, it doesn't match some of the hashes.

Without hashing it will work like a charme but i need a solution with hashed attachments.

Have someone an idea to solve my problem?

puQ
  • 31
  • 7

1 Answers1

0

You can read the file using .path and then assign the file object:

new_image.logo = File.open(post.logo.path)
new_image.save
arieljuod
  • 15,460
  • 2
  • 25
  • 36
  • I tried it, but strangely enough the calculated hash in the path is not matching an existing one .. – puQ May 13 '17 at 07:55
  • Are you sure the file is where it's suposed to be? the url config is set to "url: '/test/post/logo'" without a hash, it looks like the path config shouldn't have the :hash or you have to add the :hash wildcard to the url config – arieljuod May 13 '17 at 16:18
  • also, the url does not have an :id wildcard! – arieljuod May 13 '17 at 16:21
  • The hash will be automatically added to the path. If i try to access a logo through an self build absolute path i got the following error: 'source sequence is illegal/malformed utf-8' And yes i am sure the file is where i supposed it to be. I can add the :hash to the path config for sure but this will generate the same result as the described above one. – puQ May 15 '17 at 17:03