I have a rails 3 app with paperclip. If the attachment is an image, and the image width or height is under 100, I want to skip it from being saved.
I have the following in the model:
validate :file_dimensions, :unless => "errors.any?"
def file_dimensions
dimensions = Paperclip::Geometry.from_file(attachment.to_file(:original))
if dimensions.width < 100 || dimensions.height < 100
errors.add(:file,'Width or height must be at least 100px')
end
end
Problem is I don’t want to raise an error because then the entire job fails, I just want paperclip to skip that attachment from being saved, and not stop the entire process.
You claim you don’t want to raise an error so you can save. In that case just use a
:before_validatecallback:Of course if you want to show an error message to the user you will need to pass it through in an instance var or something (obviously you can’t use errors or it won’t save).
The attachment.dirty? line above is a small enhancement so that you aren’t constantly checking the attachment every time you save the model, but only when a new attachment is uploaded. This is particularly important if you store files on S3.