Img source: getapp.com

CarrierWave is a Ruby gem that provides a really easy way to upload files from Ruby applications. UploadCare is similar to it, but with a small difference: When you upload an image using UploadCare, then you will receive a CDN (Content Delivery Network) URL, which will be saved in your database.

If you have different models in your application where one model uses UploadCare, and the other one uses CarrierWave, then there is a simple way to do the conversion from UploadCare’s prepared link to the type that CarrierWave supports.

I am going to use this UploadCare gem, as it makes it really easy to use this service out of the box with very little effort.

Let’s take 2 models and see how we can do this conversion: 

  • User, which has the image attribute of CarrierWave type
  • Candidate, which has the avatar attribute of UploadCare type

Now let’s suppose that we have a scenario that when someone registers in our web application using LinkedIn, then he is going to get the status of a Candidate, and his avatar image will be a CDN link.

When he pays an extra fee for using our services, he is going to get the status of a User and his User image is going to be a CarrierWave field. In this case, we will need to download the image from the CDN URL and then save it in the database.

CarrierWave simplifies the upload of an image via an extra method that it adds to a model, such as remote_image_url.

Let’s implement the registration of this new premium user in the database.


def create
    candidate = Candidate.find(params[:id])
    user = candidate
    user.remote_image_url = candidate.avatar
    user.save
  end

That’s it. There is nothing else that you need to do in your back end, aside from the need to invoke this method when it is supposed, which in our case is when a candidate pays the extra fee.

CarrierWave downloads the image from the URL that you have provided and saves you a lot of time to need to do all that by yourself.

I hope this is something that helps you.

Happy coding.