views:

67

answers:

1

Hi,

I have strange situation - when I'm trying to update model, that can contains attachment by Paperclip (I'm using postgres as db) with the following code I see errors:

>> u.profile.avatar = nil
=> nil
>> u.save!
ActiveRecord::RecordInvalid: Avatar file size file size must be between 0 and 1048576 bytes.
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/validations.rb:1090:in `save_without_dirty!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/dirty.rb:87:in `save_without_transactions!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:200:in `save!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:182:in `transaction'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:200:in `save!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/transactions.rb:200:in `save!'
        from (irb):8
        from :0

This situation is strange because it's working on sqlite3.

My model contains:

  has_attached_file :avatar, 
    :path => ':rails_root/public/assets/images/avatars/:id/:style/:basename.:extension',
    :url => ApplicationSettings.assets_host_url + '/assets/images/avatars/:id/:style/:basename.:extension',
    :default_style => :thumb,
    :default_url => ':default_url',
    :whiny_thumbnails => true,
    :styles => {
        :tiny => ['32x32>', :png],
        :small => ['75x75>', :png],
        :regular => ['100x100>', :png],
      }

  validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/gif']
  validates_attachment_size :avatar, :less_than => 1.megabytes

Can you help me please?

+1  A: 

Alison R. has posted a comment to the solution, but let me write a complete solution.

Try this:

validates_attachment_size :avatar, :less_than => 1.megabytes,
  :unless => Proc.new {|model| model.avatar }
egarcia
Thanks, it's working :)
Alexey Poimtsev