Migrate CarrierWave image data to another model in Roby on Rails

Ats
2 min readNov 5, 2023

--

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.

Photo by Robinson Greig on Unsplash

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!

--

--

Ats
Ats

Written by Ats

I like building something tangible like touch, gesture, and voice. Ruby on Rails / React Native / Yocto / Raspberry Pi / Interaction Design / CIID IDP alumni

No responses yet