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.