Last week, I had to create a new model and migrate image data from another model in Ruby on Rails project. This is the note about what I did for that.
First of all
There are several uploader libraries for Ruby on Rails. I’m talking about CarrierWave in this article.
What I did
I used the remote_xxx_url
method provided by CarrierWave.
Long story short, the method downloads the image from a URL and uploads it to the cloud. The following parts are the details about how the method works.
I’ll show you the code line details below. I executed the code in a batch script.
user = User.last
new_user = NewUser.create!(remote_avatar_url: user.avatar_url)
I still need to keep the old image URLs so I haven’t deleted them from our S3. But if you want to do that, use remove_xxx!
method.
That’s it!