How to validate attachment per model not per uploaded

For different objects I could have different sizes of images. For profile picture I would like to limit the size to 1MB. For other pictures I would like to allow 10M.

I could add validation but it is validation for the whole image uploaded as in

class ImageUploader < Shrine
  Attacher.validate do
    # ... perform validation ... 

Does this mean I must have two different image uploaders - one for Profile where the check is for 1MB and one for other where the check is for 10MB?

Your question doesn’t contain all the necessary info so I’m assuming you have 2 AR models and you want to use the same ImageUploader for both but validate with 2 different file sizes based on the model.

In the validate block, you have access to attacher, file, record, name, and context which you can use with an if statement to determine what size to validate. See Validation Usage.

Yes, they are 2 Active Records.

So should I do?

Attacher.validate do
    if record == "User"
      validate for one size
    elsif record == "Article"
      validate another size

Is there a way to put that data of the image size in the ActiveRecord


    include ImageUploaded::Attachment(:image, 10mb) 

The validation methodology looks fine. but i would probably switch to two different uploaders for code clarity if things become any more complicated.

Is there a way to put that data of the image size in the ActiveRecord

Yes there is - use the metadata_attributes plugin. There is an example in the link which handles size: your exact situation. please make sure you have a migration with the appropriate column name set in place.