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 8518547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:00:34+00:00 2026-06-11T06:00:34+00:00

This is my Image model, in which I’ve implemented a method for validating the

  • 0

This is my Image model, in which I’ve implemented a method for validating the attachment’s dimensions:

class Image < ActiveRecord::Base
  attr_accessible :file

  belongs_to :imageable, polymorphic: true

  has_attached_file :file,
                     styles: { thumb: '220x175#', thumb_big: '460x311#' }

  validates_attachment :file,
                        presence: true,
                        size: { in: 0..600.kilobytes },
                        content_type: { content_type: 'image/jpeg' }

  validate :file_dimensions

  private

  def file_dimensions(width = 680, height = 540)
    dimensions = Paperclip::Geometry.from_file(file.queued_for_write[:original].path)
    unless dimensions.width == width && dimensions.height == height
      errors.add :file, "Width must be #{width}px and height must be #{height}px"
    end
  end
end

This works fine, but it’s not reusable since the method takes fixed values for width & height. I want to transform this to a Custom Validator, so I can use it in other models too. I’ve read the guides about this, I know it’ll be something like this in app/models/dimensions_validator.rb:

class DimensionsValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    dimensions = Paperclip::Geometry.from_file(record.queued_for_write[:original].path)

    unless dimensions.width == 680 && dimensions.height == 540
      record.errors[attribute] << "Width must be #{width}px and height must be #{height}px"
    end
  end
end

but I know I’m missing something cause this code doesn’t work. The thing is that I want to call the validation like this in my model:

validates :attachment, dimensions: { width: 300, height: 200}.

Any idea on how this validator should be implemented?

  • 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-06-11T06:00:35+00:00Added an answer on June 11, 2026 at 6:00 am

    Put this in app/validators/dimensions_validator.rb:

    class DimensionsValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        # I'm not sure about this:
        dimensions = Paperclip::Geometry.from_file(value.queued_for_write[:original].path)
        # But this is what you need to know:
        width = options[:width]
        height = options[:height] 
    
        record.errors[attribute] << "Width must be #{width}px" unless dimensions.width == width
        record.errors[attribute] << "Height must be #{height}px" unless dimensions.height == height
      end
    end
    

    Then, in the model:

    validates :file, :dimensions => { :width => 300, :height => 300 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a Order and image model which look like this: class Order
I have this model which has Image field to be uploaded. It has a
I have a activerecord model called game with an paperclip attachment called image. I
Using Symfony 1.4.5 with Doctrine I have a model which includes an uploaded image
I have a model news item which contains text, image etc to display as
Say I have the following models: class Image(models.Model): image = models.ImageField(max_length=200, upload_to=file_home) content_type =
I wanna have an Image_Model which defines x, y, width, height, name,... This model
This is my class in essence import mx.controls.Image; public class ImageFrameView extends Image {
My problem is the next: I have a method which validate the image dimension
Here's what I'm trying to do, and failing... I have a File model which

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.