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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:33:15+00:00 2026-06-18T12:33:15+00:00

The model: class Attachment < ActiveRecord::Base belongs_to :narrative attr_accessible :description, :user_id, :narrative_id has_attached_file :file

  • 0

The model:

class Attachment < ActiveRecord::Base

  belongs_to :narrative

  attr_accessible :description, :user_id, :narrative_id

  has_attached_file :file

  validates_presence_of :user_id
  validates_presence_of :narrative_id
  validates_attachment :file, :presence => true,
                       :size => {:less_than => 20.megabytes}
end

The test which doesn’t work:

describe Attachment do
  it { should validate_presence_of :file }
  it { should validate_size_of :file } # validate_size_of does not exist
end

I would like to avoid dumping a 20 MB file into the repo just to test this. Is there a way similar to the one I tried above that will actually work?

  • 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-18T12:33:16+00:00Added an answer on June 18, 2026 at 12:33 pm

    The best way I’ve done this is to use the built-in shoulda matchers for Paperclip. The documentation at that link is really good, but here’s an overview from the docs of what you can do with it:

    In spec_helper.rb, you’ll need to require the matchers:

    require "paperclip/matchers"
    

    And include the module:

    Spec::Runner.configure do |config|
      config.include Paperclip::Shoulda::Matchers
    end
    

    Example that validates the attachment size:

    describe User do
      it { should validate_attachment_size(:avatar).
                    less_than(2.megabytes) }
    end
    

    If you’re interested, the source for the matchers can be found on GitHub

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

Sidebar

Related Questions

I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for
I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,
I have a Meeting model: class Meeting < ActiveRecord::Base has_one :location, :class_name => MeetingLocation,
I have a User model which has_many experiments: class User < ActiveRecord::Base has_many :experiments,
i'm using nested_form for adding and deleting nested_attributes. class Text < ActiveRecord::Base attr_accessible :attachments_attributes
My model: class User has_many :items end class Item belongs_to :user def self.with_color(color, is_loaded)
I have two models nested with accepts_nested_attributes_for : class Place < ActiveRecord::Base # Relations..
I have 2 models (Book & Image) class Book < ActiveRecord::Base has_many :images accepts_nested_attributes_for
I have a simple has_many attachments situation: class Project < ActiveRecord::Base has_many :images, :class_name
#model class Promotion(models.Model): name = models.CharField(max_length=200) start_date = models.DateTimeField() end_date = models.DateTimeField() #view def

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.