latest paperclip version - validations borked?

17 views
Skip to first unread message

Timo Rößner

unread,
Jun 11, 2010, 11:06:55 AM6/11/10
to Paperclip Plugin
Hey guys,

weird problem with the latest paperclip-gem and rails both 2 and 3 -
apparently a

validates_attachment_content_type :foo
validates_attachment_size :foo....

adds some kind of implicit

validates_presence_of :foo

as well?

Probably the best is to show you some code:

( $ ) bundle show rails
/usr/lib/ruby/gems/1.8/gems/rails-3.0.0.beta4
( $ ) bundle show paperclip
~/.bundle/ruby/1.8/gems/paperclip-2.3.3

Profile-model:

class Profile < ActiveRecord::Base
has_attached_file :icon, :styles => { :small => '80x80#', :medium =>
'120x120#' }
validates_attachment_size :icon, :less_than => 2.megabytes
validates_attachment_content_type :icon, :content_type => ['image/
png', 'image/pjpeg', 'image/jpeg', 'image/gif']
end

Profile-Factory:

Factory.define :profile do |p|
p.real_name { Random.firstname + ' ' + Random.lastname }
p.country Random.country
p.description Random.paragraphs
p.personal_website_url 'www.google.com'
p.twitter_name Random.firstname
end

This code is running with paperclip 2.3.1.1 and rails 2.3.5 as
expected, however, with the gem-versions I showed at the beginning it
gives some surprising results:

p = Factory(:profile)
ActiveRecord::RecordInvalid: Validation failed: Icon file size file
size must be between 0 and 2097152 bytes., Icon content type is not
one of image/png, image/pjpeg, image/jpeg, image/gif
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/validations.rb:46:in `save!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/attribute_methods/dirty.rb:30:in `save!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/transactions.rb:240:in `save!'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/transactions.rb:287:in
`with_transaction_returning_status'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/connection_adapters/abstract/database_statements.rb:
139:in `transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/transactions.rb:202:in
`transaction'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/transactions.rb:285:in
`with_transaction_returning_status'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/
active_record/transactions.rb:240:in
`save!'
from ~/.bundle/ruby/1.8/bundler/gems/
factory_girl-511843606ed9fcb0d1005a7b7b4e9598b07eae20-
feac7298352a83fef0717d8beadd2eda9aabfe56/lib/factory_girl/proxy/
create.rb:6:in
`result'
from ~/.bundle/ruby/1.8/bundler/gems/
factory_girl-511843606ed9fcb0d1005a7b7b4e9598b07eae20-
feac7298352a83fef0717d8beadd2eda9aabfe56/lib/factory_girl/factory.rb:
323:in `run'
from ~/.bundle/ruby/1.8/bundler/gems/
factory_girl-511843606ed9fcb0d1005a7b7b4e9598b07eae20-
feac7298352a83fef0717d8beadd2eda9aabfe56/lib/factory_girl/factory.rb:
267:in `create'
from ~/.bundle/ruby/1.8/bundler/gems/
factory_girl-511843606ed9fcb0d1005a7b7b4e9598b07eae20-
feac7298352a83fef0717d8beadd2eda9aabfe56/lib/factory_girl/factory.rb:
298:in `send'
from ~/.bundle/ruby/1.8/bundler/gems/
factory_girl-511843606ed9fcb0d1005a7b7b4e9598b07eae20-
feac7298352a83fef0717d8beadd2eda9aabfe56/lib/factory_girl/factory.rb:
298:in `default_strategy'
from ~/.bundle/ruby/1.8/bundler/gems/
factory_girl-511843606ed9fcb0d1005a7b7b4e9598b07eae20-
feac7298352a83fef0717d8beadd2eda9aabfe56/lib/factory_girl.rb:21:in
`Factory'
from (irb):1

Doesnt matter if I use a factory or do it by hand:

p = Profile.create
p.errors
=> #<OrderedHash {:icon_content_type=>["is not one of image/png, image/
pjpeg, image/jpeg, image/gif"], :icon_file_size=>["file size must be
between 0 and 2097152 bytes."]}>

These validations shouldn't kick in, since I didn't specify an
attachment at all, and they _don't_ kick in using older versions -
what has happened here?

P.S.:

I had the same problem with rails 2.3.5 and the latest paperclip gem -
so this problem apparently has nothing to do with rails 3.

P.PS.:

When i try to use an older gem-version with rails (say 2.3.1.1) i get
the error:

DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead.
(called from expand_path at /usr/lib/ruby/gems/1.8/gems/
paperclip-2.3.1.1/lib/paperclip.rb:39)
/usr/lib/ruby/gems/1.8/gems/paperclip-2.3.1.1/lib/paperclip.rb:39:in
`expand_path': can't convert #<Class:0xb734607c> into String
(TypeError)

so I guess i need to solve this problem for the latest paperclip-
version.
Message has been deleted

Timo Rößner

unread,
Jun 15, 2010, 6:56:42 AM6/15/10
to Paperclip Plugin
(First of all, sorry if any of you have the identical message already
in their mail-box - I was logged in with the wrong gmail-account, so
sorry for spamming)

So nobody's experiencing the same problems?

As I already mentioned, I can get the very same Code from above
running with

- rails 2.3.5
- paperclip 2.3.1.1

but NOT with

- rails 2.3.5
- paperclip 2.3.3

so this seems to be a paperclip issue.

Any idea would be highly appreciated.

On Jun 11, 5:06 pm, Timo Rößner <dark.red.and.yel...@googlemail.com>
wrote:

Timo Rößner

unread,
Jun 16, 2010, 12:01:34 PM6/16/10
to Paperclip Plugin
Update:

Jake Paul suggested this:

"See comment 105: http://railscasts.com/episodes/134-paperclip#comment_98867"

which is:

---------------------------
For 'validates_attachment_content_type', you can add :allow_nil =>
true. For 'validates_attachment_size', you can use :allow_nil => true
after a quick fix as follows:
Open your paperclip.rb plugin file.
Find 'def validates_attachment_size name' string.
Replace 'validates_inclusion_of' rows with the following:
validates_inclusion_of :"#{name}_file_size", options.merge(:in =>
range, :message => message)
---------------------------

Unfortunately, this didnt help.
:allow_nil => true fixed the problem with
'validates_attachment_content_type' but the second suggestion did not
solve the validates_attachment_size-issue.

I also tried the first few suggestions from here:
http://www.google.com/search?q=allow_nil+paperclip+validate

But they turned out to have no effect.

To me, this seems like a serious bug in paperclip.

Any more suggestions?

On Jun 15, 12:56 pm, Timo Rößner <dark.red.and.yel...@googlemail.com>

Timo Rößner

unread,
Jun 16, 2010, 12:08:23 PM6/16/10
to Paperclip Plugin
Next update:

This seems to be the same issue as here: http://github.com/thoughtbot/paperclip/issues/issue/131

On Jun 16, 6:01 pm, Timo Rößner <dark.red.and.yel...@googlemail.com>

Jonathan Yurek

unread,
Jun 18, 2010, 12:05:01 AM6/18/10
to papercli...@googlegroups.com
As I just mentioned in the other thread about validation problems, I've pushed a fix up to master which should address these concerns.

> --
> Individuals over processes. Interactions over tools.
>
> Agile Rails training from thoughtbot, the makers of Paperclip, Clearance, Shoulda, & Factory Girl:
> http://thoughtbot.com/services/training
>
> The Paperclip group:
> http://groups.google.com/group/paperclip-plugin
>
> To post to this group, send email to
> papercli...@googlegroups.com
>
> To unsubscribe from this group, send email to
> paperclip-plug...@googlegroups.com

--
Jonathan Yurek, Founder and CTO
thoughtbot, inc.
organic brains. digital solutions.

617.482.1300 x114
http://www.thoughtbot.com/

Timo Rößner

unread,
Jun 21, 2010, 8:27:48 AM6/21/10
to Paperclip Plugin
Hey Jonathan,

just updated paperclip and the validations are working again as
expected - thanks for the quick bug-fix.

Cheers
Reply all
Reply to author
Forward
0 new messages