Skip to content

Shoulda matcher for validate_attachment_presence failing #1194

christoomey opened this Issue Mar 13, 2013 · 8 comments

7 participants


I have the following class and spec in a rails app. The specs pass if I just use has_attached_file :document, but the validation spec fails if I provide the :styles arg as shown below.

# app/models/attachment.rb
class Attachment < ActiveRecord::Base
  attr_accessible :document
  # passing in the styles option causes validation spec to fail
  has_attached_file :document, styles: { :thumb => ["32x32#", :png] } 
  validates :document, :attachment_presence => true

# spec/models/attachment.rb
require 'spec_helper'

describe Attachment do
  it { should have_attached_file(:document) }
  it { should validate_attachment_presence(:document) }
xaethos commented Mar 14, 2013

This happens after upgrading ImageMagick to 6.8.0. My specs were passing with 6.7.1.

thoughtbot, inc. member
jyurek commented Mar 15, 2013

I have 6.8.0 installed and it doesn't seem to be a problem. @xaethos, what versions of everything else do you have: Ruby, Rails, RSpec, Paperclip, etc?

The only thing I can find that reliably made an error was using Paperclip 3.2.1. Switching to 3.4.1 made it work. But @christoomey said he's using 3.4.1 and it doesn't work. So I don't know. Maybe it's something else.

xaethos commented Mar 25, 2013

The problem is with the matcher itself. It is checking for any error on the field instead of the presence error: "can't be blank."
The problem gets triggered because the matcher tries setting a text file as the attachment. When you have something like:

has_attached_file :document, styles: { :thumb => ["32x32#", :png] }

The file gets sent to ImageMagick for postprocessing, which in turn generates an error because it can't handle the text file.

It's not really important that ImageMagick errors out: since the error attached to the field is not "can't be blank," the test should pass anyway.

I'll try to make a pull request, but I've been really busy lately. If somebody wants to beat me to it, please go ahead.


Is this problem still present in latest gem version? I would like to solve it.
Which Ruby, Rails, RSpec etc. versions will I need to reproduce it? Will edge ones be enough?

conzett commented Apr 13, 2014

I just ran into this in a Rails 4.0 app:

  • Shoulda 3.5
  • Paperclip 3.4.2
  • rspec 2.14

Hi everybody! Is this still an issue for you in Paperclip; I know this issue is from approximately 2 years ago. If it is still an issue, can you please provide the code that's causing you the error? Thanks!

thoughtbot, inc. member
tute commented May 15, 2015

@christoomey @jyurek is this still an issue with Paperclip?

thoughtbot, inc. member
jyurek commented May 18, 2015

I think, given that this hasn't been mentioned in two years in a serious manner, that it's probably not an issue anymore.

@jyurek jyurek closed this May 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.