Search code examples
ruby-on-railsvalidationruby-on-rails-4.2ruby-on-rails-5

Rails: validate all date fields in all models globally


I am working on an issue where we have a large number of models with date fields. Looking for a way to have a validation that applies to all date fields across the project, and validates the date is before Dec 31, 9999.

The only solutions I have found involve going through and adding specific validations to each model, but my project lead would prefer a cleaner single-location solution and I can’t find anything that does this.


Solution

  • Just add a base class

    class Base < ActiveRecord::Base
      validates :date, presence: true
    end
    

    Then every model inherits the base class