Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:41:38+00:00 2026-05-21T06:41:38+00:00

I have a rails 3 app with paperclip. If the attachment is an image,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:41:39+00:00Added an answer on May 21, 2026 at 6:41 am

    You claim you don’t want to raise an error so you can save. In that case just use a :before_validate callback:

    before_validate :check_file_dimensions
    
    def check_file_dimensions
      if attachment.dirty?
        dimensions = Paperclip::Geometry.from_file(attachment.to_file(:original))
        if dimensions.width < 100 || dimensions.height < 100
          attachment.clear
        end
      end
    end
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple rails app and i use paperclip gem for my image
I have a RESTful resource in my Rails app called Photo. I'm using Paperclip
I have a rails app with Devise 1.4.9. Currently, it allows only users from
I have a rails app running on Heroku. I am using paperclip for some
So I have a Rails 3 app using Paperclip to crop images. I have
I have a Rails app with Users, and each user HABTM Roles. I want
I've just added video uploading with paperclip to a Rails 3 app I have
I have a rails 3 app with paperclip. The model looks a little like:
I have a rails 3 app using paperclip. Is there a way with paperclip
I have a rails app. I'm using gem:paperclip to upload photos to my server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.