← Back to Upcase

Validations on a model which has time type column

(charlieanna) #1

I have a ClassTiming model which has two columns, “from” with type time and “to” with type time. Time should be represented only with hours and minutes and no seconds.

How do I validate such that the from is always less than the to column.

(Matthew Sumner) #2

From the rails guides, you can create a method to check the validity of a model and add messages to the errors. You can then call it with validate.


class ClassTiming < ActiveRecord::Base
  validates_presence_of :from, :to
  validate :from_must_be_before_to

  def from_must_be_before_to
    errors.add(:from, "must be before to") unless
      from < to

(charlieanna) #3

Thanks @DeviousDuck