Our Blog

Paperclip - attachment validations

Using Paperclip, if you need to check if your model has an attachment, you don't use the standard ActiveRecord validation helper i.e.;

  validates_presence_of :logo

Erroneous!

Instead you use Paperclip's helper

  validates_attachment_presence :logo

But be sure to put that after your attachment definition

  has_attached_file :logo
  validates_attachment_presence :logo

And if you're doing something tricky, like allowing your model to be saved without an attachment first, but enforcing that an attachment is present on update then; 

  validates_attachment_presence :logo, :unless => :new_record?

Loading mentions Retweet
Posted by Glenn Roberts 

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     twitter