Use one uploader of image (carrierwave) for several models in Rails?

carrierwave, ruby-on-rails

Solution

It's absolutely fine to use two separate uploaders, even if they're nearly identical. They all extend `CarrierWave::Uploader::Base` anyway, it just keeps your objects nice and neatly named.

If you have some shared functionality you want to use in both and don't want to repeat, you could always write your own class that extends `CarrierWave::Uploader::Base` and inherit from those in your uploaders instead!

class MyImageUploaderBase < CarrierWave::Uploader::Base
  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

class AvatarUploader < MyImageUploaderBase 
  ...
end

Problem

Here I have two models: User and Book User has a Avatar to upload, and Book has a Cover to upload I have read the railscast about uploading images, but I dont know how should I approach different type of images? (In this case, the avatar for User and cover for Book) I have checked out some open source code for forums. It uses a AvatarUploader and a PhotoUploader, which is to upload photos in a post. So, Should I use one uploader both for photos and avatars(And How? ) or I just create 2 uploaders for each? Thank you !

Original source