Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Sequel model to have validation for create and not update

Tags:

ruby

sequel

How I can I avoid having validation for update while having it for create? For instance:

I wish to have an image field to have presence validation on create. But want to avoid it in edit and assume the previous value in case of no change.

Note: I am using Padrino.

like image 541
Jikku Jose Avatar asked Oct 16 '14 07:10

Jikku Jose


Video Answer


1 Answers

In Sequel, validations are generally done at the instance level using the validation_helpers plugin. So you just use a standard ruby conditional if you only want to validate it for new objects and not for existing ones:

plugin :validation_helpers
def validate
  super
  validates_presence :image if new?
end
like image 106
Jeremy Evans Avatar answered Oct 08 '22 03:10

Jeremy Evans