Search code examples
rubysequel

Ruby Sequel model to have validation for create and not update


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.


Solution

  • 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